[Haskell-beginners] A little explanation!

Kyle Murphy orclev at gmail.com
Wed Apr 30 18:16:45 UTC 2014


On the point of it being "retarded" there's some merit to that in that it's
written in point-free (or as some joking call it, pointless) style. The
idea is to remove the explicit variables and so the functions are
re-arranged and composed such that all the variables come at the end of the
statement and you're left with nothing but a chain of functions at the
start (which can sometimes be easier to understand since it's just function
composition). The downside, as you've seen is that it often hurts
readability and so there are some that are strongly opposed to point-free
style. If used carefully, and things are broken into smaller easier to
understand chunks (via for instance let or where clauses) it can still be
readable even using point-free style, but it's really dependent on both the
skill of the person writing the statement, and the familiarity and skill of
the person reading the statement. When in doubt about legibility it's
better to avoid point-free style, in particular if you start having to do
strange things like apply the (.) operator to itself (E.G. "(f .) . g" or
similar) you're probably being to clever for your own good. I wouldn't go
so far as to say you should *never* use point-free style, but you should
seriously consider if you're trying to just be clever and show off, or if
it really is making the statement simpler to understand via more clearly
showing how the functions are composed.


-R. Kyle Murphy
--
Curiosity was framed, Ignorance killed the cat.


On Wed, Apr 30, 2014 at 1:19 PM, Michael Orlitzky <michael at orlitzky.com>wrote:

> 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).
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140430/a6f4888a/attachment-0001.html>


More information about the Beginners mailing list