Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining functions pointfree-style in functional programming. What are the cons/pros?

Every time I write something of the form

let scorePopulation f population =
  Array.map (fun i -> f i) population

I end up asking myself if wouldn't I be better writing

let scorePopulation f =
  Array.map (fun i -> f i)

instead. Is there any advantage of writing it in the first form as opposed to the second?

Also, how would you call a function definition in the first form and in the second form?

like image 590
devoured elysium Avatar asked Sep 09 '11 21:09

devoured elysium


2 Answers

You could go even further:

let scorePopulation = Array.map

In terms of advantages/disadvantages, I think that the main concern will typically be readability. Sometimes it's easier to understand what a function does when all of the arguments are present. Other times, the fact that you don't need to make up extraneous variable names wins out.

like image 104
kvb Avatar answered Oct 20 '22 11:10

kvb


Actually, the pros and cons are the same: naming.

The reason for this is the old saying by Phil Karlton:

There are only two hard problems in computer science. Cache invalidation and naming things.

If naming things is hard (i.e. expensive), then names shouldn't be wasted on irrelevant things. And conversely, if something has a name, then it is important.

Point-free style allows you to omit names.

The pro is that it allows you to omit irrelevant names.

The con is that it allows you to omit relevant names.

like image 24
Jörg W Mittag Avatar answered Oct 20 '22 11:10

Jörg W Mittag