What is "point free" style (in Functional Programming)?
Asked Answered
P

7

136

A phrase that I've noticed recently is the concept of "point free" style...

First, there was this question, and also this one.

Then, I discovered here they mention "Another topic that may be worth discussing is the authors' dislike of point free style."

What is "point free" style? Can someone give a concise explanation? Does it have something to do with "automatic" currying?

To get an idea of my level - I've been teaching myself Scheme, and have written a simple Scheme interpreter... I understand what "implicit" currying is, but I don't know any Haskell or ML.

Pomerania answered 3/6, 2009 at 12:28 Comment(1)
Just a note: to see why it's called pointfree visit Pointfree/But pointfree has more points! at HaskellWiki.Maugham
M
77

Just look at the Wikipedia article to get your definition:

Tacit programming (point-free programming) is a programming paradigm in which a function definition does not include information regarding its arguments, using combinators and function composition [...] instead of variables.

Haskell example:

Conventional (you specify the arguments explicitly):

sum (x:xs) = x + (sum xs)
sum [] = 0

Point-free (sum doesn't have any explicit arguments - it's just a fold with + starting with 0):

 sum = foldr (+) 0

Or even simpler: Instead of g(x) = f(x), you could just write g = f.

So yes: It's closely related to currying (or operations like function composition).

Mozart answered 3/6, 2009 at 12:38 Comment(5)
In what way is it related to Currying?Gaekwar
@kaleidic: Because without having variable names, you need to compose partially applied functions. That's what we call currying (or, more precisely, what is made possible through currying)Mozart
Don't you mean sum (x:xs) ... instead of sum sum (x:xs) ... ?Insnare
Can you explain why we need the parens, and we can't simply use sum = foldr + 0 ?Zing
@Zing because then it thinks it's adding foldr and 0, so parentheses are necessary for infix functions in haskellCatarrhine
M
49

Point-free style means that the arguments of the function being defined are not explicitly mentioned, that the function is defined through function composition.

If you have two functions, like

square :: a -> a
square x = x*x

inc :: a -> a
inc x = x+1

and if you want to combine these two functions to one that calculates x*x+1, you can define it "point-full" like this:

f :: a -> a
f x = inc (square x)

The point-free alternative would be not to talk about the argument x:

f :: a -> a
f = inc . square
Misha answered 3/6, 2009 at 12:46 Comment(3)
Stupidly, in Haskell, the 'point-free' way is usually the one that looks pointier (more periods). This annoyance makes an excellent mnemonic. (The book Real World Haskell comments on this.)Haberman
Concerning @Dan's comment, the Pointfree HaskellWiki page offers an explanation of why it is called pointfree.Rubato
@Dan: I don't think it's stupid, as the Haskell point is meant to be "that circle operator" (should look more like ° though). But confusing, it is, especially when you are new to functional programming languages; every intro book on haskell should explain point-free-style.Armful
D
20

A JavaScript sample:

//not pointfree cause we receive args
var initials = function(name) {
  return name.split(' ').map(compose(toUpperCase, head)).join('. ');
};

const compose = (...fns) => (...args) => fns.reduceRight((res, fn) => [fn.call(null, ...res)], args)[0];
const join = m => m.join();

//pointfree
var initials = compose(join('. '), map(compose(toUpperCase, head)), split(' '));

initials("hunter stockton thompson");
// 'H. S. T'

Reference

Dissuade answered 25/4, 2017 at 20:12 Comment(1)
The link has changed to github.com/MostlyAdequate/mostly-adequate-guide/blob/master/… But I can't make the code runSteinway
H
8

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.

For instance:

myTake = take

returns a function that takes one argument, therefore there is no reason to explicit type the argument unless you just want too.

Harmonica answered 6/6, 2009 at 14:14 Comment(1)
Sometimes, it doesn't work in Haskell 98, as in myShow = show. There's more about it on the Haskell wikiInsnare
N
2

The best explanation for this is in Haskell wiki (excerpt below), pointed out by Petr in a comment.

In the declaration

f x = x + 1

we define the function f in terms of its action on an arbitrary point x. Contrast this with the points-free version:

f = (+ 1)

where there is no mention of the value on which the function is acting.

The point is a mathematical point (x above), hence "points-free" notation. The link gives more detail, if you need.

Navigator answered 31/7, 2022 at 1:41 Comment(0)
S
1

I can't make the javascript sample provided Brunno work, although the code illustrate pointfree idea (i.e. no arguments) clearly. So I use ramda.js to provide another example.

Say I need to find out the longest word in a sentence, given a string "Lorem ipsum dolor sit amet consectetur adipiscing elit" I need output something like { word: 'consectetur', length: 11 }

If I use plain JS style code I will code like this, using a map and a reduce function

let str = 'Lorem ipsum dolor sit amet consectetur adipiscing elit'
let strArray = str.split(' ').map((item) => ({ word: item, length: item.length }))
let longest = strArray.reduce(
    (max, cur) => (cur.length > max.length ? cur : max), 
    strArray[0])
console.log(longest)

With ramda I still use a map & a reduce but I will code like this

const R = require('ramda')
let longest = R.pipe(
  R.split(' '),
  R.map((item) => ({ word: item, length: item.length })),
  R.reduce((max, cur) => (max.length > cur.length ? max : cur), { length: 0 })
)
let tmp = longest(str)
console.log(tmp)

I will argue that the gist of my ramda code is the pipe that chains my functions together and it makes my purpose more clearly. No need to create a temporary variable strArray is a bonus (if I have more than 3 steps in the pipe then it will become a real bonus).

Steinway answered 8/12, 2020 at 7:31 Comment(0)
A
0

Here is one example in TypeScript without any other library:

interface Transaction {
  amount: number;
}

class Test {
  public getPositiveNumbers(transactions: Transaction[]) {
    return transactions.filter(this.isPositive);

    //return transactions.filter((transaction: {amount: number} => transaction.amount > 0));
  }

  public getBigNumbers(transactions: Transaction[]) {
    // point-free
    return transactions.filter(this.moreThan(10));

    // not point-free
    // return transactions.filter((transaction: any) => transaction.amount > 10);
  }

  private isPositive(transaction: Transaction) {
    return transactions.amount > 0;
  }

  private moreThan(amount: number) {
    return (transaction: Transaction) => {
      return transactions.amount > amount;
    }
  }
}

You can see point-free style is more "fluent" and easier to read.

Atalya answered 22/2, 2018 at 0:12 Comment(3)
That's not point-free style, that's just distinction between a lambda and a named function.Directive
@Directive I think you missed the point, this.moreThan(10) is not a named function, it's a curried function as well as a function that'll implicitly (thus point free) take a transaction as its input.Atalya
using a class as an example for tacit style is not the best choice, thoughCareful

© 2022 - 2024 — McMap. All rights reserved.