Scheme higher-order procedures
last modified November 24, 2026 Guile 3.0
A higher-order procedure takes a procedure as an argument or
returns one as its result. Scheme procedures are values like numbers and
strings, as the expressions and evaluation
article showed, so passing them around needs no special syntax. This article
covers the standard higher-order procedures map,
for-each, filter, and apply, writes a
fold, builds procedures from other procedures, and ends with a small data
pipeline. The examples run with GNU Guile 3.0.7.
Procedures as arguments
A parameter can hold a procedure, and the body calls it like any other
procedure. The twice procedure applies its first argument two
times.
(define (twice f x) (f (f x))) (define (double x) (* 2 x)) (define (exclaim s) (string-append s "!")) (display (twice double 5)) (newline) (display (twice exclaim "Hello there")) (newline) (display (twice (lambda (x) (* x x)) 3)) (newline) (display (twice cdr '(a b c d))) (newline)
twice does not know what f does; it only calls it.
With double, 5 becomes 10 and then 20, and with
exclaim the string gets two exclamation marks. An anonymous
lambda can be passed directly, without a name. Built-in procedures
are values too, so (twice cdr '(a b c d)) drops two elements.
$ guile twice.scm 20 Hello there!! 81 (c d)
map
map calls a procedure on every element of a list and returns a new
list of the results, in the same order. With several lists, the procedure
receives one element from each list.
(define (square x) (* x x))
(write (map square '(1 2 3 4 5)))
(newline)
(write (map string-length '("pear" "plum" "cherry")))
(newline)
(write (map (lambda (n) (* n 1.2)) '(10 20 30)))
(newline)
(write (map + '(1 2 3) '(10 20 30)))
(newline)
(write (map cons '(a b c) '(1 2 3)))
(newline)
(write (map list '(x y) '(1 2) '("one" "two")))
(newline)
(write (map square '()))
(newline)
string-length turns a list of strings into a list of numbers.
(map + '(1 2 3) '(10 20 30)) adds the lists element by element,
and (map cons ...) pairs up keys and values. The procedure must
take as many arguments as there are lists: list takes any number,
so it combines three lists into a list of triples. Mapping over the empty list
returns the empty list.
$ guile mapping.scm (1 4 9 16 25) (4 4 6) (12.0 24.0 36.0) (11 22 33) ((a . 1) (b . 2) (c . 3)) ((x 1 "one") (y 2 "two")) ()
for-each
for-each walks the lists like map but does not
collect any results. It is used for side effects, such as printing or
updating a variable, and the elements are visited from first to last.
(define planets '(mercury venus earth mars))
(for-each (lambda (p)
(display p)
(newline))
planets)
(for-each (lambda (name moons)
(display name)
(display ": ")
(display moons)
(newline))
planets
'(0 0 1 2))
(define total 0)
(for-each (lambda (x) (set! total (+ total x))) '(4 8 15 16 23 42))
(display "total: ")
(display total)
(newline)
The first loop prints each planet on its own line. The second one takes two
lists and prints each planet with its number of moons. The third loop adds the
numbers into total with set!. Its value is printed
afterwards; the value that for-each itself returns is unspecified
and should not be used.
$ guile foreach.scm mercury venus earth mars mercury: 0 venus: 0 earth: 1 mars: 2 total: 108
Writing map and for-each
map and for-each are ordinary procedures, and
one-list versions take a few lines. Writing them shows that there is nothing
magic about a procedure parameter.
(define (transform f lst)
(if (null? lst)
'()
(cons (f (car lst))
(transform f (cdr lst)))))
(define (visit f lst)
(unless (null? lst)
(f (car lst))
(visit f (cdr lst))))
(write (transform (lambda (x) (+ x 1)) '(1 2 3)))
(newline)
(write (transform symbol->string '(red green blue)))
(newline)
(visit (lambda (w)
(display (string-upcase w))
(display " "))
'("one" "two" "three"))
(newline)
transform follows the list recursion pattern from the
recursion article: an empty list maps to an
empty list, and otherwise the transformed first element is consed onto the
transformed rest. visit calls f for its effect and
moves on; unless stops at the end of the list.
$ guile transform.scm
(2 3 4)
("red" "green" "blue")
ONE TWO THREE
filter
filter takes a predicate and a list and returns the elements for
which the predicate is true, in their original order. Guile provides it
without an import.
(define numbers '(12 7 -3 0 25 8 -14 31))
(write (filter odd? numbers))
(newline)
(write (filter negative? numbers))
(newline)
(write (filter (lambda (n) (> n 10)) numbers))
(newline)
(define words '("sky" "mountain" "sea" "valley" "river"))
(write (filter (lambda (w) (> (string-length w) 4)) words))
(newline)
(define (keep-if pred lst)
(cond ((null? lst) '())
((pred (car lst))
(cons (car lst) (keep-if pred (cdr lst))))
(else (keep-if pred (cdr lst)))))
(write (keep-if even? numbers))
(newline)
(write (map (lambda (n) (* n n)) (filter positive? numbers)))
(newline)
The built-in predicates odd? and negative? are passed
by name, and a lambda expresses other conditions. Filtering
works on any kind of element, here strings longer than four characters.
keep-if is a hand-written filter that uses
cond to decide whether to keep the first element. The last line
chains two higher-order procedures: it keeps the positive numbers and squares
them.
$ guile filtering.scm
(7 -3 25 31)
(-3 -14)
(12 25 31)
("mountain" "valley" "river")
(12 0 8 -14)
(144 49 625 64 961)
Folding a list
A fold combines all elements of a list into one value. It starts from an initial value and merges the elements into it one at a time. A left fold processes the elements from the first to the last, and a right fold combines them starting from the end.
(define (fold-from-left f acc lst)
(if (null? lst)
acc
(fold-from-left f (f acc (car lst)) (cdr lst))))
(define (fold-from-right f init lst)
(if (null? lst)
init
(f (car lst) (fold-from-right f init (cdr lst)))))
(define data '(3 1 4 1 5 9 2 6))
(display (fold-from-left + 0 data))
(newline)
(display (fold-from-left * 1 data))
(newline)
(display (fold-from-left max (car data) (cdr data)))
(newline)
(write (fold-from-left (lambda (acc x) (cons x acc)) '() '(a b c)))
(newline)
(write (fold-from-right cons '() '(a b c)))
(newline)
(write (fold-from-left - 0 '(1 2 3)))
(display " ")
(write (fold-from-right - 0 '(1 2 3)))
(newline)
fold-from-left is tail-recursive and passes the running result in
acc; fold-from-right builds a chain of calls that is
resolved from the end of the list. Folding with +,
*, and max gives the sum, the product, and the
largest element. A left fold that conses reverses the list, while a right fold
with cons copies it. The direction matters for operations that are
not associative: the left fold computes ((0 - 1) - 2) - 3 and the right fold
1 - (2 - (3 - 0)). The SRFI 1 list library
provides ready-made fold, fold-right, and
reduce.
$ guile folding.scm 31 6480 9 (c b a) (a b c) -6 2
apply
apply calls a procedure with the elements of a list as its
arguments. It is the way to pass a list to a procedure that takes a variable
number of arguments, such as +, max, or
string-append.
(define scores '(78 92 65 88 71))
(display (apply + scores))
(newline)
(display (apply max scores))
(display " ")
(display (apply min scores))
(newline)
(display (apply string-append '("Bra" "ti" "sla" "va")))
(newline)
(display (apply + 1 2 '(3 4 5)))
(newline)
(write (apply map list '((1 2 3) (4 5 6))))
(newline)
(define (average . nums)
(/ (apply + nums) (length nums)))
(display (apply average scores))
(newline)
(display (exact->inexact (apply average scores)))
(newline)
(apply + scores) is the same as (+ 78 92 65 88 71).
Arguments between the procedure and the final list are passed first, so
(apply + 1 2 '(3 4 5)) adds all five numbers.
(apply map list '((1 2 3) (4 5 6))) passes the two rows as
separate lists to map, which transposes the matrix. average takes a
rest parameter, and apply spreads the scores into it. The result
is the exact rational 394/5, converted for display with
exact->inexact.
$ guile applying.scm 394 92 65 Bratislava 15 ((1 4) (2 5) (3 6)) 394/5 78.8
Returning procedures
A procedure can create and return a new procedure. The returned
lambda remembers the arguments of the call that created it, so
one definition produces a whole family of procedures.
(define (make-adder n) (lambda (x) (+ x n))) (define (make-multiplier n) (lambda (x) (* x n))) (define add10 (make-adder 10)) (define triple (make-multiplier 3)) (display (add10 5)) (display " ") (display (triple 5)) (newline) (write (map add10 '(1 2 3))) (newline) (write (map (make-multiplier 100) '(1 2 3))) (newline) (display ((make-adder 1) 41)) (newline) (define (flip f) (lambda (a b) (f b a))) (display ((flip -) 2 10)) (display " ") (write ((flip cons) 'tail 'head)) (newline)
(make-adder 10) returns a procedure that adds 10 to its
argument, and (make-multiplier 3) one that triples it. The results
can be named, passed to map, or called at once, as in
((make-adder 1) 41). flip takes a two-argument
procedure and returns one with the arguments swapped. A procedure that keeps
access to the variables of its creator is a closure; the
closures article explores them in depth.
$ guile returning.scm 15 15 (11 12 13) (100 200 300) 42 8 (head . tail)
Composition
Composing two procedures yields a procedure that applies one and then passes
the result to the other. Guile provides compose together with a
few small helpers: negate inverts a predicate, const
returns a procedure that ignores its arguments, and identity
returns its argument unchanged.
(define (compose2 f g)
(lambda (x) (f (g x))))
(define (inc x) (+ x 1))
(define (square x) (* x x))
(display ((compose2 square inc) 4))
(display " ")
(display ((compose2 inc square) 4))
(newline)
(define shout (compose string-upcase symbol->string))
(display (shout 'hello))
(newline)
(define sum-of-squares (compose (lambda (lst) (apply + lst))
(lambda (lst) (map square lst))))
(display (sum-of-squares '(1 2 3 4)))
(newline)
(write (filter (negate odd?) '(1 2 3 4 5 6)))
(newline)
(write (map (const 0) '(a b c)))
(newline)
(write (map identity '(x y z)))
(newline)
compose2 is a two-procedure version written by hand. Order
matters: squaring after incrementing 4 gives 25, incrementing after squaring
gives 17. Guile's compose takes any number of procedures and
applies them from right to left, so shout first converts the
symbol to a string and then uppercases it. sum-of-squares is
built entirely from smaller procedures. (negate odd?) keeps the
even numbers, and const and identity are useful as
arguments where a procedure is required.
$ guile composing.scm 25 17 HELLO 30 (2 4 6) (0 0 0) (x y z)
Partial application
Partial application fixes some arguments of a procedure and returns a
procedure that waits for the rest. A curry helper can be written
with rest parameters and apply. SRFI 26, which Guile ships, adds
the cut form, where <> marks the open
argument slots.
(use-modules (srfi srfi-26))
(define (curry f . first-args)
(lambda rest-args
(apply f (append first-args rest-args))))
(define greet
(curry string-append "Hello there, "))
(display (greet "Lucia"))
(newline)
(write (map (curry * 2) '(1 2 3)))
(newline)
(write (map (cut * 2 <>) '(1 2 3)))
(newline)
(write (map (cut - <> 1) '(1 2 3)))
(newline)
(write (map (cut list 'pt <> <>) '(1 2) '(3 4)))
(newline)
(write (filter (cut > <> 2) '(1 2 3 4)))
(newline)
(display ((cut string-append "tags: " <...>) "a" "b" "c"))
(newline)
The use-modules form at the top loads SRFI 26; modules are
covered in the modules article. curry collects the first arguments,
and the returned procedure appends the remaining ones before calling
f, so greet always starts with the same prefix.
cut is more flexible because the slot may be anywhere:
(cut - <> 1) subtracts 1 from its argument, and
(cut > <> 2) tests whether a value is greater than 2.
Each <> is a separate parameter, and a final
<...> accepts any number of remaining arguments.
$ guile currying.scm Hello there, Lucia (2 4 6) (2 4 6) (0 1 2) ((pt 1 3) (pt 2 4)) (3 4) tags: abc
A data pipeline
Higher-order procedures combine into pipelines that read like a description of the task. The program keeps a small product inventory as a list of lists and answers several questions about it.
(define products
'(("keyboard" 45 3)
("monitor" 230 2)
("mouse" 15 10)
("cable" 4 25)
("headset" 60 0)))
(define (name p) (car p))
(define (price p) (cadr p))
(define (quantity p) (caddr p))
(define (stock-value p)
(* (price p) (quantity p)))
(define in-stock
(filter (lambda (p) (> (quantity p) 0)) products))
(for-each (lambda (p)
(display (name p))
(display ": ")
(display (stock-value p))
(newline))
in-stock)
(display "total value: ")
(display (apply + (map stock-value in-stock)))
(newline)
(display "cheap items: ")
(write (map name (filter (lambda (p) (< (price p) 20)) products)))
(newline)
(display "most valuable: ")
(display (apply max (map stock-value products)))
(newline)
Small accessors name the fields of a product. filter selects the
products in stock, for-each prints their stock value, and the
total is the sum of the mapped values. The cheap items come from a filter
followed by a map of the names, and apply max finds
the most valuable stock. None of the steps uses an explicit loop.
$ guile pipeline.scm
keyboard: 135
monitor: 460
mouse: 150
cable: 100
total value: 845
cheap items: ("mouse" "cable")
most valuable: 460
Source
List Mapping — GNU Guile Reference Manual
SRFI 26: Notation for Specializing Parameters without Currying
In this article we have passed procedures as arguments, transformed lists with
map, visited them with for-each, selected elements
with filter, written left and right folds, spread lists into calls
with apply, and built new procedures by returning, composing, and
partially applying others. The closures
article continues with procedures that carry state, and the
SRFI 1 list library article covers the extended
list procedures.
Author
List all Scheme tutorials.