14

Many haskell programmers, including me, like pointless style, especially when writing complicated parsers. They make code more readable and less verbose. But sometimes, it's just the other way round (for instance while abusing the instances of Monad and friends for (->) a).

Please give me some basic guideline, when do you think that pointless style is useful and when not. For instance, I always use a lambda if I had to use a partial composition instead (something like flip ((.) . take) . drop).

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
fuz
  • 88,405
  • 25
  • 200
  • 352
  • 17
    When did "pointless" take over "point-free" as the way to refer to this style? Because I have to say, I find it really amusing to refer to one's code as "pointless." – CodexArcanum Mar 14 '11 at 21:52
  • 2
    I think it's mostly because lambdabot in freenode:#haskell calls it that, mostly as a jocular observation that it does often make code less readable. – geekosaur Mar 14 '11 at 21:56
  • 1
    http://stackoverflow.com/questions/2464406/point-free-in-haskell – sclv Mar 14 '11 at 22:39
  • http://stackoverflow.com/questions/944446/what-is-point-free-style-in-functional-programming – sclv Mar 14 '11 at 22:40
  • 3
    @CodexArcanum, the first time I've seen "pointless" was in The Evolution of a Haskell Programmer http://www.willamette.edu/~fruehr/haskell/evolution.html – sastanin Mar 14 '11 at 22:56

2 Answers2

14

This is obviously a matter of personal style. I think that pointfree style is a tool for clarifying your ideas, and viewing (->) a as a Monad and (->) as an Arrow is a good thing if it serves that purpose.

I can think of one do and one don't:

  • Don't compose with curried composition, it's just too complicated to dissect,e.g.,(sort .) . (++) is best written \xs ys -> sort (xs ++ ys).
  • Do use any combinator from the standard Control.* modules, e.g., write curry (negate *** (+1)) using (->) as an Arrow and ap (zipWith (+)) tail using (->) [a] as a Monad.

The reason to involve combinators from common control types isn't just to make your meaning clear but also it reminds you that these exists and are often useful, not only for making pointfree definitions but also for solving problems.

As with all things one should be careful not to over do it. A pointfree definition involving too many combining functions can quickly become complicated.

pat
  • 12,587
  • 1
  • 23
  • 52
HaskellElephant
  • 9,819
  • 4
  • 38
  • 67
12

If in doubt, just compose functions with (.). Don't step into flips and swaps and (.) . (.)s

Don Stewart
  • 137,316
  • 36
  • 365
  • 468