Lambdas and composition

Lambdas

A lambda is a function without a name. The backslash stands in for the Greek letter, the arguments follow, and an arrow separates them from the body:

map (\x -> x * x) [1,2,3] -- [1,4,9] filter (\s -> length s > 3) ["cat","horse"] -- ["horse"] zipWith (\a b -> a * b) [1,2] [10,20] -- [10,40]

Use one when the function is short and used once. As soon as the same lambda appears twice, or it grows past one line, give it a name in a where block.

Composition

The dot operator joins two functions into one. It reads right to left, so the rightmost function runs first:

import Data.Char (toUpper) oddLength = odd . length oddLength "abc" -- True shout = map toUpper . filter (/= ' ') shout "hi there" -- "HITHERE"

Composition is associative, so f . (g . h) and (f . g) . h are the same function. That is what lets a long pipeline be split across lines without changing its meaning.

Point-free style

When the last argument appears on both sides, it can be dropped. The result is called point-free, and it is a matter of taste, not of correctness:

-- with the argument named total xs = sum (map (*2) xs) -- the same function, point-free totalPointFree = sum . map (*2) totalPointFree [1,2,3] -- 12, the same as total [1,2,3]

The second version says what the function is rather than what it does to an argument. It reads well for three or four steps and badly beyond that.

Where it hurts

Point-free style fails when an argument is used twice or in an unusual position. Fighting the compiler to remove a name is a sign that the name should stay:

-- clear describe x = show x ++ " (" ++ show (x * 2) ++ ")" -- the point-free attempt uses x twice, so it does not compile -- describeBad = ((++) . (" (" ++) . show . (*2) . (")" ++)) . show

Describe.hs:3:43: error: [GHC-39999] • No instance for ‘Num [Char]’ arising from a use of ‘*’ • In the expression: (*) In the first argument of ‘(.)’, namely ‘(* 2)’ In the second argument of ‘(.)’, namely ‘(* 2) . (")" ++)’ | 3 | describeBad = ((++) . (" (" ++) . show . (*2) . (")" ++)) . show | ^ Describe.hs:3:61: error: [GHC-39999] • Ambiguous type variable ‘a0’ arising from a use of ‘show’ prevents the constraint ‘(Show a0)’ from being solved. Relevant bindings include describeBad :: a0 -> [Char] -> [Char] (bound at Describe.hs:3:1) Probable fix: use a type annotation to specify what ‘a0’ should be. Potentially matching instances: instance Show Ordering -- Defined in ‘GHC.Internal.Show’ instance Show Integer -- Defined in ‘GHC.Internal.Show’ ...plus 25 others ...plus 13 instances involving out-of-scope types (use -fprint-potential-instances to see them all) • In the second argument of ‘(.)’, namely ‘show’ In the expression: ((++) . (" (" ++) . show . (* 2) . (")" ++)) . show In an equation for ‘describeBad’: describeBad = ((++) . (" (" ++) . show . (* 2) . (")" ++)) . show | 3 | describeBad = ((++) . (" (" ++) . show . (*2) . (")" ++)) . show | ^^^^

The second version uses the argument twice, so no chain of compositions can remove the name. Fighting the compiler to remove a name is a sign that the name should stay. Write the version a colleague can read at two in the morning.

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

Article author: Arthur Isaev