Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use pointless style?

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).

like image 382
fuz Avatar asked Mar 14 '11 21:03

fuz


People also ask

What is point free code?

Point-free style means programming where you don't have to name arguments or intermediate values. The main reason we do it is to be able to code at the right level of abstraction. You don't want to be thinking about, "How do I name this function?" or "How do I name this argument?" Stuff like that.

What is point free Haskell?

Point free style means that the code doesn't explicitly mention it's arguments, even though they exist and are being used. This works in Haskell because of the way functions work.


1 Answers

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.

like image 108
HaskellElephant Avatar answered Sep 28 '22 07:09

HaskellElephant