So something like
addList :: [int] -> int
addList = foldl1 (+)
Why does this work? The Currying part. Why no variable?
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 (+)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With