I’m working on the 99 Haskell questions and saw a solution for finding the last element of a list:
myLast = foldr1 (const id)
the type of const is a -> b -> a but that of const id is b -> a -> a
so what’s the magic here?
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.
The type of
idisc->c; it just returns the same thing it is given.The type of
constisa->b->a. Inconst id,abecomesc->c, so the type of const in this case becomes:Now that you have applied this const function, i.e. you have passed
idto it, then you are left withb -> (c->c).PS: The type of
const const idisa->b->a, and the type ofconst const const idisb->a->a, and so on!