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
Post a Comment