(.), ($) and point-free code

Composition with .

(.) takes two functions and returns the function that applies the second and then the first: (f . g) x = f (g x). It is infixr 9, so a chain f . g . h groups from the right and reads in the order the data flows: h first, then g, then f.

(negate . abs) (-5) -- -5 map (negate . abs) [-1, 2, -3] -- [-1,-2,-3] (sum . map (* 2)) [1, 2, 3] -- 12 length . filter even -- a function waiting for a list

The last line is the point: composition builds a new function without mentioning an argument, and that function can be passed to map, stored in a let, or given a name. The composition of total functions is total, and the composition of two pure functions is pure, so the algebra works without exceptions.

Application with $

($) is function application as an operator: f $ x = f x. It exists to save parentheses, because it is infixr 0: everything on its right is evaluated before it, so print $ sum $ map (* 2) xs needs no brackets at all.

print (sum (map (* 2) [1, 2, 3])) -- 12 print $ sum $ map (* 2) [1, 2, 3] -- 12, the same line print . sum . map (* 2) $ [1, 2, 3] -- 12, composition and $

The comparison is the lesson: the first line is a stack of parentheses, the second removes them, and the third replaces the rightmost application with a composition. The value is the same in all three, and the choice is about how a reader follows the data. A rule that works: use $ when the last argument is a list or a block, and composition when the whole thing is a function value.

Point-free code

A definition that never names its argument is called point-free or tacit. It is the natural result of composition, and it is worth it exactly while the name of the function says more than the argument would:

sumOfDoubles :: [Int] -> Int sumOfDoubles = sum . map (* 2) sumOfDoubles' :: [Int] -> Int sumOfDoubles' xs = sum (map (* 2) xs)

The first is shorter and says what it is: sum after doubling. The second names xs once and would also work if the body were longer. Both compile to the same code, and the compiler checks the types the same way; the difference is for the reader, not the machine.

When point-free stops helping

Point-free code becomes a puzzle when the argument appears twice, or when the composition hides a pattern match. The test is simple: if you have to count arrows to read it, name the argument.

-- clear average xs = sum xs / fromIntegral (length xs) -- the point-free version of the same, which nobody wants to read average = uncurry (/) . (sum &&& fromIntegral . length)

The second version is legal and uses &&& from Control.Arrow, and it is a good illustration of the limit: the argument xs is used twice, so the point-free form needs a combinator that duplicates it. The course writes point-free definitions where the flow is a single chain and named arguments everywhere else.

A second, longer program shows the chain in a real pipeline: split a string on spaces, drop empty words, and join the result back with a marker.

module Main (main) where words' :: String -> [String] words' = filter (not . null) . splitOn ' ' splitOn :: Char -> String -> [String] splitOn _ "" = [] splitOn c s = let (word, rest) = break (== c) s in word : case rest of [] -> [] (_ : more) -> splitOn c more main :: IO () main = do print (words' "a b c") print (map length (words' "a bbbb c") :: [Int]) putStrLn . unwords . map (++ "!") $ words' "hi there"

ghc -Wall -O1 -o pipeline pipeline.hs
./pipeline

["a","b","c"]
[1,4,1]
hi! there!

The last line is the composition in its natural habitat: take the words, add an exclamation mark to each, join them with spaces, print the line. Read from the right, it is the order the data moves; read from the left, it is the order the functions are written. Both readings are the same chain.

Exercises

  1. Rewrite print (length (filter even [1 .. 10])) with $ and then with composition.
  2. Write sumOfSquares = sum . map (^ 2) and check its type in GHCi.
  3. Compose reverse and take 3 in both orders and explain the difference in the result.
  4. Write average with a named argument and then the point-free version from the lesson; say which one you would commit.
  5. Use flip ($) to write a pipeline as a left-to-right chain and compare it with the right-to-left composition.
  6. Explain why (.) is right associative and what changes if you read f . g . h the other way.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does (f . g) x mean?

    Answer

    f (g x)

    Composition applies the right-hand function first and passes its result to the left-hand one.

  2. What does (negate . abs) (-5) evaluate to?

    Answer

    -5

    abs turns -5 into 5 and negate turns it back into -5, so the composition looks like the identity on this input.

  3. What is the type of ($)?

    Answer

    (a -> b) -> a -> b

    ($) is application as an operator: it takes a function and a value and applies one to the other.

  4. What does this program print? module Main (main) where main :: IO () main = print (sum . map (* 2) $ [1, 2, 3] :: Int)

    Answer

    12

    The list is doubled to [2,4,6] and summed, which is 12.

  5. Why does $ let you remove parentheses?

    Answer

    It is infixr 0, so everything on its right is the second argument

    With the lowest precedence and right associativity, the whole right-hand side becomes the argument of the function on the left.

  6. What is point-free code?

    Answer

    A definition that never names its argument

    Point-free means the argument is left implicit and the function is built by composition and partial application.

  7. When does point-free code stop helping?

    Answer

    When the argument is used twice or the chain needs counting to read

    A repeated argument needs a combinator such as &&&, and the result is harder to read than a named argument.

  8. What does this program print? module Main (main) where main :: IO () main = print (map (negate . abs) [-1, 2, -3] :: [Int])

    Answer

    [-1,-2,-3]

    abs makes every number positive and negate makes every result negative.

  9. Which reading of f . g . h matches the data flow?

    Answer

    h first, then g, then f

    The composition is right associative, so the rightmost function runs first.

  10. What does this program print? module Main (main) where main :: IO () main = print (length . filter even $ ([1 .. 6] :: [Int]))

    Answer

    3

    The even numbers are 2, 4 and 6, and length counts three of them.

The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev