Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 38 ยท Higher-order Functions

Function Composition and Currying

Build small functions into bigger ones with compose, curry and partial application in Racket and Scheme. Write your own helpers and see functions that make functions.

Once functions are values, you can write functions that build other functions. Two patterns come up constantly. Composition chains functions so the output of one feeds the next: f then g. Currying (and its cousin partial application) fixes some arguments of a function in advance and returns a new function waiting for the rest. Both let you assemble programs out of small, reusable pieces.

Run examples define these helpers by hand, which works in any Scheme. Real Racket also provides compose, curry and thunk in racket/function; those are shown read-only.

Composition

Suppose you often need "add one, then double". Instead of writing a lambda each time, write a function that composes two functions:

RACKET
(define (compose2 f g)
  (lambda (x) (f (g x))))

(define add1-then-double (compose2 (lambda (x) (* 2 x)) add1))
(add1-then-double 5)    ; 12

The order follows mathematics: (compose2 f g) means f after g, so g runs first. In Racket the built-in compose accepts any number of functions:

RACKET
(require racket/function)
((compose string-upcase symbol->string) 'hello)   ; "HELLO"
(map (compose add1 add1) '(1 2 3))                ; '(3 4 5)
Compose by hand

The last helper takes any number of functions (that is what the dot in (compose . fs) means; the next lesson covers it). Notice how foldl builds the chain: it reads the functions left to right, wrapping each new one around what came before, so (compose add1 double add1) means "add1 after double after add1".

Partial application

Partial application means giving a function some of its arguments now and getting back a function for the rest. You can do it with a closure:

RACKET
(define (add a b) (+ a b))
(define (partial-add a) (lambda (b) (add a b)))
(define add3 (partial-add 3))
(add3 10)   ; 13

This is exactly make-adder from the closures lesson. A general helper works for any function and any fixed arguments:

RACKET
(define (partial f . fixed)
  (lambda rest (apply f (append fixed rest))))

(define add10 (partial + 10))
(add10 5)         ; 15
(map (partial * 2) '(1 2 3))   ; '(2 4 6)

Racket's built-in curry does this, and curryr fixes the last arguments instead:

RACKET
((curry + 10) 5)           ; 15
(map (curry * 2) '(1 2 3)) ; '(2 4 6)
((curryr - 1) 10)          ; 9   10 - 1
Partial application

Currying

A curried function takes its arguments one at a time: f takes a and returns a function that takes b. In some languages (Haskell) every function is curried. In Racket, functions take all their arguments at once, but you can write a curried version when it helps:

RACKET
(define (curried-add a)
  (lambda (b) (+ a b)))
((curried-add 3) 4)   ; 7

Curried style pays off when you want to configure a function first and apply it many times:

RACKET
(define (greater-than n) (lambda (x) (> x n)))
(filter (greater-than 10) '(5 15 25 8))   ; '(15 25)
(map (curried-add 100) '(1 2 3))          ; '(101 102 103)
Predicates that make predicates

both combines two predicates into a new one: a tiny example of function combinators, functions that take functions and return a function. A little library of combinators (both, either, negate) lets you describe conditions the way you describe them in words.

A function that makes pipelines

Putting it together, here is a pipe that runs a value through a list of steps, left to right (the opposite reading order of compose):

A tiny pipe

In real Racket the threading library and the built-in ~> (from racket/function extensions such as the threading-lib package) express the same idea with syntax, but the underlying mechanism is exactly this fold.

Thunks

A thunk is a function of no arguments: (lambda () expr). It delays computing expr until you call it, and is useful for lazy values and callbacks:

RACKET
(define (delayed-add a b) (lambda () (+ a b)))
(define t (delayed-add 1 2))
(t)    ; 3, computed only now

Racket's thunk macro writes this as (thunk (+ 1 2)).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In (compose2 f g), which function runs first?

    compose2 means f after g: g is applied to the input first, then f to the result.
  2. 2. What does (partial + 10) return?

    Partial application fixes the first argument and returns a function waiting for the rest.
  3. 3. What is a thunk?

    A thunk wraps an expression in (lambda () ...), so it runs only when called.
  4. 4. What is a function combinator?

    Combinators such as compose or both build new functions from existing ones.

Summary

  • Composition chains functions: (compose f g) is f after g.
  • Partial application and currying fix some arguments and return a function awaiting the others.
  • Racket supplies compose, curry, curryr and thunk; each is easy to build yourself with closures.
  • Functions that build predicates and pipelines read almost like descriptions of the task.

Next: apply, Variadic Functions and Optional Arguments