Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

haskell

What is the point-free style in Haskell?

Muhammad Hameed

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

We can use the point-free style or tacit programming to write programs where function definitions don’t have arguments. We combine other functions to build new functions rather than explicitly declaring arguments. In other words, we define the function through function composition.

Point-free functions perform the same function as normal functions. Here are some advantages of point-free functions:

  • They are more compact.
  • They are easier to understand.
  • They help discard irrelevant information easily.

Syntax

let fn x = f (g (h x))

In the code above, we write a simple function definition without using point-free.

let fn = f . g . h

In this piece of code, we make use of point-free.

As seen above, it’s easy to understand the function defined on the right compared to the code on the left.

Implementation in Haskell

Let’s implement point-full and point-free styles in Haskell and observe the difference.

Point-full style

f :: Int -> Int
f x = x + 1
z :: Int -> Int
z x = x * x
g x = f(z x)
main = print (g(4))
Code using point-full style

Code explanation

In the code above we implement the function xx * xx +1+1 without point-free. The simple functions are combined together using a third function declaration which is gg.

The highlighted line in the code above shows the integration of the two functions ff and zz.

Point-free style

f :: Int -> Int
f x = x + 1
z :: Int -> Int
z x = x * x
g = f . z
main = print (g(4))
Code using point-free style

Code explanation

We’ve calculated xx * xx +1+1. Here, we use the function ff to compute x+1x+1. We use the function z to compute the square of whatever the argument is.

The primary function is gg, which combines ff and zz. If we see the highlighted line in both windows, we can see how the function g has no arguments in the latter windows and hence, is point-free.

When we combine multiple function declarations as one using point-free, it makes it easier to read codes. It also helps understand better, as shown below, given that multiple functions are being used.

Example

summ :: Int -> Int
summ x = x + x
squared :: Int -> Int
squared x = x * x
subtractt :: Int -> Int
subtractt x = x - 5
fin = summ . squared . subtractt
main = print (fin(4))
A simple code that uses point-free

Code explanation

In this example shown above we use point free to implement the function (x(x - 5)5) 2+^2 + (x(x - 5)5) 2^2. First, the function, subtractt is called and then the result is squared. The result of this function is then added to itself using point-free. The function integration of all these 3 functions is done using a fourth function fin as shown in the highlighted line above.

RELATED TAGS

haskell

CONTRIBUTOR

Muhammad Hameed
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring