haskell - How is this function equivalent to getting the last item in a list? -


after completing first problem ("find last element of list") in 99 questions exercises, wanted see how solution compared others , found this solution.

mylast' = foldr1 (const id) 

this documentation seems show foldr1 takes 2 arguments, first being function , second being list. definition appears take function argument. there implicit definition of arguments passed this?

mylast' xs = foldr1 (const id) xs 

i have looked definitions of foldr1, const, , id, i'm having hard time understanding how 3 work return last item in list.

you're right. in haskell, function takes 2 arguments can treated function takes 1 argument , returns function takes argument; known currying. note function signature foldr1 is:

(a -> -> a) -> [a] -> 

while think of "a function takes function , list arguments , returns value", it's "a function takes function argument , returns function takes list , returns value".


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -