ZetCode

Scheme SRFI 1 list library

last modified November 24, 2026 Guile 3.0

The standard list procedures cover the basics, and SRFI 1 adds a large, consistent library on top of them. SRFIs, Scheme Requests for Implementation, are community specifications shared by many implementations, and SRFI 1 is among the most widely supported. Guile ships it as the module (srfi srfi-1). This article walks through its main groups of procedures. The higher-order procedures article is a good preparation, because most of them take a procedure argument. The examples run with GNU Guile 3.0.7.

Loading the library

The library is loaded with use-modules at the top of the file; modules are covered in a later article. The simplest additions are named selectors and tests for the shape of a list.

accessors.scm
(use-modules (srfi srfi-1))

(define planets '(mercury venus earth mars jupiter saturn))

(write (first planets))
(display " ")
(write (second planets))
(display " ")
(write (third planets))
(newline)
(write (sixth planets))
(newline)
(write (last planets))
(newline)
(write (list (proper-list? planets) (proper-list? '(1 . 2))))
(newline)
(write (list (null-list? '()) (null-list? planets)))
(newline)

first through tenth are clearer names for car, cadr, caddr, and the rest, and last returns the final element, not the last pair as last-pair does. proper-list? is SRFI 1's name for list?: it is false for the dotted pair (1 . 2). null-list? tests for the end of a list. Guile already provides a few SRFI 1 procedures without the import, such as iota, filter, and delete; loading the module makes the whole set available.

$ guile accessors.scm
mercury venus earth
saturn
saturn
(#t #f)
(#t #f)

Building lists

iota creates a list of numbers from a count, an optional start, and an optional step. list-tabulate calls a procedure on each index to produce the elements.

building.scm
(use-modules (srfi srfi-1))

(write (iota 5))
(newline)
(write (iota 5 1))
(newline)
(write (iota 6 0 5))
(newline)
(write (iota 4 1.0 0.5))
(newline)
(write (list-tabulate 5 (lambda (i) (* i i))))
(newline)
(write (list-tabulate 3 (lambda (i) (make-list (+ i 1) 'x))))
(newline)
(write (cons* 1 2 '(3 4)))
(newline)
(write (append-reverse '(3 2 1) '(4 5)))
(newline)

(iota 5) counts from 0, (iota 5 1) from 1, and (iota 6 0 5) in steps of five. With an inexact start, the elements are inexact. list-tabulate squares the indices in the first call and builds lists of growing length in the second. cons* is like list except that its last argument becomes the tail of the result, and append-reverse reverses its first argument onto the front of the second.

$ guile building.scm
(0 1 2 3 4)
(1 2 3 4 5)
(0 5 10 15 20 25)
(1.0 1.5 2.0 2.5)
(0 1 4 9 16)
((x) (x x) (x x x))
(1 2 3 4)
(1 2 3 4 5)

Taking and dropping

take returns the first k elements of a list and drop the rest. The -right variants count from the end, and the -while variants stop at the first element that fails a predicate.

slicing.scm
(use-modules (srfi srfi-1))

(define data '(4 8 15 16 23 42))

(write (take data 2))
(display " ")
(write (drop data 2))
(newline)
(write (take-right data 2))
(display " ")
(write (drop-right data 2))
(newline)
(write (take-while even? data))
(display " ")
(write (drop-while even? data))
(newline)

(define (page lst size n)
  (take (drop lst (* size n)) size))

(write (page (iota 12) 4 0))
(display " ")
(write (page (iota 12) 4 2))
(newline)

take-while keeps 4 and 8 and stops at 15, the first odd number, and drop-while returns the list from there. page combines both to cut a list into pages of a given size and return one of them. SRFI 1 also has split-at, span, and break, which return both parts at once as two values; the multiple values article uses them.

$ guile slicing.scm
(4 8) (15 16 23 42)
(23 42) (4 8 15 16)
(4 8) (15 16 23 42)
(0 1 2 3) (8 9 10 11)

fold and fold-right

fold is the left fold and fold-right the right fold. Unlike the hand-written folds of the higher-order procedures article, they pass the element first and the accumulated value second, and they accept several lists.

folding.scm
(use-modules (srfi srfi-1))

(define data '(3 1 4 1 5 9 2 6))

(display (fold + 0 data))
(newline)
(write (fold cons '() '(a b c)))
(newline)
(write (fold-right cons '() '(a b c)))
(newline)
(write (fold (lambda (x acc) (if (> x 3) (+ acc 1) acc)) 0 data))
(newline)
(write (fold (lambda (x acc) (if (> x acc) x acc)) (car data) data))
(newline)
(write (fold-right (lambda (x acc) (if (odd? x) (cons x acc) acc))
                   '() data))
(newline)
(write (fold (lambda (a b acc) (+ acc (* a b))) 0 '(1 2 3) '(4 5 6)))
(newline)
(write (fold-right list 'init '(1 2 3)))
(display " ")
(write (fold list 'init '(1 2 3)))
(newline)

(fold + 0 data) sums the list. Folding with cons reverses it, because fold calls (cons element acc) from the first element on, while fold-right with cons copies the list. The next three folds count the elements above 3, find the maximum, and keep the odd numbers in order. With two lists, the procedure receives an element of each plus the accumulator, which gives a dot product. The last line uses list to show the shape of the calls each fold makes.

$ guile folding.scm
31
(c b a)
(a b c)
4
9
(3 1 1 5 9)
32
(1 (2 (3 init))) (3 (2 (1 init)))

reduce

reduce is a fold that uses the first element as the initial value. Its second argument is returned only when the list is empty, so it can be a neutral value that never takes part in the computation.

reducing.scm
(use-modules (srfi srfi-1))

(define data '(3 1 4 1 5 9 2 6))

(display (reduce + 0 data))
(display " ")
(display (reduce max 0 data))
(display " ")
(display (reduce max 0 '()))
(newline)
(write (reduce list 'none '(1 2 3)))
(display " ")
(write (reduce-right list 'none '(1 2 3)))
(newline)
(write (reduce-right append '() '((1 2) (3) () (4 5))))
(newline)
(write (reduce string-append "" '("a" "b" "c")))
(display " ")
(write (fold string-append "" '("a" "b" "c")))
(newline)

(reduce max 0 data) finds the largest element without the risk that the initial 0 would win over a list of negative numbers; it is returned only for the empty list. The two list calls show that reduce works from the left and reduce-right from the right. reduce-right with append flattens one level of nesting. The last line shows a trap of the argument order: each step calls (string-append element acc), so both reduce and fold put the strings in reverse order.

$ guile reducing.scm
31 9 0
(3 (2 1)) (1 (2 3))
(1 2 3 4 5)
"cba" "cba"

Searching

find returns the first element that satisfies a predicate, and find-tail the rest of the list starting there. any and every test whether some or all elements pass, and count counts them.

searching.scm
(use-modules (srfi srfi-1))

(define temperatures '(12 15 19 24 31 28 17))

(write (find (lambda (t) (> t 20)) temperatures))
(display " ")
(write (find (lambda (t) (> t 40)) temperatures))
(newline)
(write (find-tail (lambda (t) (> t 20)) temperatures))
(newline)
(write (list-index (lambda (t) (> t 20)) temperatures))
(newline)
(write (any (lambda (t) (> t 30)) temperatures))
(display " ")
(write (every positive? temperatures))
(display " ")
(write (every (lambda (t) (< t 30)) temperatures))
(newline)
(write (count even? temperatures))
(newline)
(write (any (lambda (t) (and (> t 20) t)) temperatures))
(newline)
(write (every < '(1 2 3) '(2 3 4)))
(newline)

The first temperature above 20 is 24, and find returns #f when nothing matches. list-index gives its position. any returns the first true value the predicate produces, not just #t, so the expression with and returns the matching temperature itself. Like map, both any and every accept several lists; the last line checks that each element of the first list is smaller than the matching one in the second.

$ guile searching.scm
24 #f
(24 31 28 17)
3
#t #t #f
3
24
#t

Removing elements

remove is the opposite of filter: it drops the elements that satisfy the predicate. delete drops elements equal to a value, and delete-duplicates keeps only the first occurrence of each element. Both use equal? unless given another equality procedure.

removing.scm
(use-modules (srfi srfi-1))

(define data '(3 1 4 1 5 9 2 6 5 3 5))

(write (remove odd? data))
(newline)
(write (delete 5 data))
(newline)
(write (delete 4 data <))
(newline)
(write (delete-duplicates data))
(newline)

(define readings '(2 -2 7 3 -7 2 -3 4))

(define (same-size? a b)
  (= (abs a) (abs b)))

(write (delete-duplicates readings))
(newline)
(write (delete-duplicates readings same-size?))
(newline)
(write (delete 7 readings same-size?))
(newline)

(delete 5 data) removes all three fives. The optional third argument is called as (= 4 element), so passing < deletes every element greater than 4. delete-duplicates keeps the order of first appearance. For the readings, equal? sees 2 and -2 as different, while same-size? compares absolute values: it keeps only the first of each pair of opposite numbers, and delete with it removes both 7 and -7. SRFI 1 also has partition, which returns the kept and the removed elements as two values.

$ guile removing.scm
(4 2 6)
(3 1 4 1 9 2 6 3)
(3 1 4 1 2 3)
(3 1 4 5 9 2 6)
(2 -2 7 3 -7 -3 4)
(2 7 3 4)
(2 -2 3 2 -3 4)

Mapping variants

append-map maps a procedure that returns lists and appends the results. filter-map maps and drops the #f results in one step. SRFI 1 also extends map and for-each to lists of unequal length.

mapping.scm
(use-modules (srfi srfi-1))

(define teams '((red anna boris) (green clara) (blue david emma)))

(write (map cdr teams))
(newline)
(write (append-map cdr teams))
(newline)
(write (append-map (lambda (x) (list x x)) '(a b c)))
(newline)

(define inputs '("12" "seven" "40" "" "3"))

(write (map string->number inputs))
(newline)
(write (filter-map string->number inputs))
(newline)
(write (filter-map (lambda (x) (and (even? x) (* x x))) '(1 2 3 4 5 6)))
(newline)
(write (map (lambda (i x) (list i x)) (iota 3) '(a b c)))
(newline)
(write (map + '(1 2 3) '(10 20)))
(newline)

map with cdr returns the members of each team as separate lists, while append-map joins them into one list. string->number returns #f for text that is not a number, so filter-map converts the valid inputs and skips the rest. The second filter-map squares only the even numbers. The next line pairs each element with its index by mapping over (iota 3) and the list together. The lists in the final call have different lengths: SRFI 1's map stops at the end of the shorter one, whereas Guile's built-in map would fail with List of wrong length.

$ guile mapping.scm
((anna boris) (clara) (david emma))
(anna boris clara david emma)
(a a b b c c)
(12 #f 40 #f 3)
(12 40 3)
(4 16 36)
((0 a) (1 b) (2 c))
(11 22)

Lists as sets

The lset- procedures treat lists as sets. The first argument is the equality procedure, which matters because sets of symbols can use eq? while sets of strings need equal? or string=?.

sets.scm
(use-modules (srfi srfi-1))

(define monday '(anna boris clara))
(define tuesday '(boris david clara emma))

(write (lset-adjoin eq? monday 'david 'anna))
(newline)
(write (lset-union eq? monday tuesday))
(newline)
(write (lset-intersection eq? monday tuesday))
(newline)
(write (lset-difference eq? tuesday monday))
(newline)
(write (lset-xor eq? monday tuesday))
(newline)
(write (lset<= eq? '(boris clara) monday))
(newline)
(write (lset= eq? '(a b c) '(c b a b)))
(newline)

lset-adjoin adds only the elements that are not present yet, so anna is not added twice. The union, intersection, and difference answer who came on either day, on both days, and only on Tuesday. lset-xor lists the people who came on exactly one day. lset<= tests for a subset and lset= for equal sets, ignoring order and repeats. The order of elements in the results is not specified by SRFI 1, so code should not depend on it.

$ guile sets.scm
(david anna boris clara)
(emma david anna boris clara)
(boris clara)
(david emma)
(david emma anna)
#t
#t

unfold

unfold is the opposite of fold: it builds a list from a seed value. It takes a stop test, a procedure that turns the seed into an element, a procedure that computes the next seed, and the first seed.

unfolding.scm
(use-modules (srfi srfi-1))

(write (unfold (lambda (n) (> n 5))
               (lambda (n) (* n n))
               (lambda (n) (+ n 1))
               1))
(newline)

(define (digits n)
  (unfold-right zero?
                (lambda (n) (remainder n 10))
                (lambda (n) (quotient n 10))
                n))

(write (digits 2026))
(newline)
(write (digits 9007199254740993))
(newline)

(define (collatz n)
  (unfold (lambda (n) (= n 0))
          (lambda (n) n)
          (lambda (n)
            (cond ((= n 1) 0)
                  ((even? n) (/ n 2))
                  (else (+ (* 3 n) 1))))
          n))

(write (collatz 6))
(newline)
(display (length (collatz 27)))
(newline)

The first call produces squares of the seeds 1 to 5 and stops when the seed exceeds 5. unfold-right builds the list from the end, which suits digits: the last digit is computed first and ends up last, and exact integers of any size work. collatz produces the Collatz sequence of a number: halve even numbers, map odd numbers to 3n + 1, and stop after reaching 1. The sequence for 27 has 112 elements.

$ guile unfolding.scm
(1 4 9 16 25)
(2 0 2 6)
(9 0 0 7 1 9 9 2 5 4 7 4 0 9 9 3)
(6 3 10 5 16 8 4 2 1)
112

Source

SRFI-1 — GNU Guile Reference Manual

SRFI 1: List Library

In this article we have loaded SRFI 1 and used its selectors, built lists with iota and list-tabulate, cut them with take and drop, combined elements with fold and reduce, searched with find, any, and every, removed elements and duplicates, used append-map and filter-map, treated lists as sets, and generated lists with unfold. The pairs and lists article covers the standard list procedures, and the association lists article uses SRFI 1 for working with key and value pairs.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Scheme tutorials.