So something like
addList :: [int] -> int
addList = foldl1 (+)
Why does this work? The Currying part. Why no variable?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you define a function like
f x y = bla, this is the same asf x = \y -> bla, which is the same asf = \x -> (\y -> bla). In other wordsfis 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 functionfwith the argumentx. This returns another function, which you apply to the argumenty.So in other words when you do
addList xs = foldl1 (+) xs, you’re first callingfoldl1 (+)which then returns another function, which you apply toxs. So since the function returned byfoldl1 (+)is actually the same one asaddList, you can just shorten it toaddList = foldl1 (+).