Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell - Currying? Need further explanation

So something like

addList :: [int] -> int
addList = foldl1 (+)

Why does this work? The Currying part. Why no variable?

like image 211
Matt Avatar asked Sep 25 '10 15:09

Matt


1 Answers

If you define a function like f x y = bla, this is the same as f x = \y -> bla, which is the same as f = \x -> (\y -> bla). In other words f is a function which takes one argument, x, and returns another function which takes one argument, y, and then returns the actual result. This is known as currying.

Analogously when you do f x y, it's the same as (f x) y. I.e. you're calling the function f with the argument x. This returns another function, which you apply to the argument y.

So in other words when you do addList xs = foldl1 (+) xs, you're first calling foldl1 (+) which then returns another function, which you apply to xs. So since the function returned by foldl1 (+) is actually the same one as addList, you can just shorten it to addList = foldl1 (+).

like image 196
sepp2k Avatar answered Sep 24 '22 08:09

sepp2k