[Haskell-beginners] A little explanation!
Michael Orlitzky
michael at orlitzky.com
Wed Apr 30 17:19:02 UTC 2014
On 04/30/2014 11:29 AM, Gilberto Melfe wrote:
> Hello everybody !
>
> Could someone explain me exactly how this function works?
>
> elementAt_w'pf = (last .) . take . (+ 1)
>
> It's a posted solution to: 99 Haskell problems, problem 3.
>
> I'm having trouble with the "(last .) ." thing!
>
It's not your fault, that solution is retarded. Once you remove all of
the intentional obfuscation, it looks like,
elementAt_w'pf n xs = last (take (n + 1) xs)
which is easy to understand. You can un-retard it step-by-step:
elementAt_w'pf = (last .) . take . (+ 1)
<=> elementAt_w'pf n = ((last .) . take . (+ 1)) n
= (last .) ((take . (+ 1)) n)
= (last .) (take (n + 1))
= last . (take (n + 1))
<=> elementAt_w'pf n xs = (last . (take (n + 1))) xs
= last (take (n + 1) xs)
All I've used above is the precedence of the function composition
operator, and the fact that (f . g) x = f (g x).
More information about the Beginners
mailing list