sigildocs

(sigil list)

(sigil list) - List Operations

Extended list operations beyond what's in the prelude. Use this module for list-specific utilities that aren't needed in every program.

Quick Start

(import (sigil list))

;; Predicates
(list-any? even? '(1 3 4))           ; => #t
(list-all? positive? '(1 2 3))       ; => #t

;; Search
(list-find even? '(1 3 4 5))         ; => 4
(list-find-index even? '(1 3 4 5))   ; => 2
(list-index 'b '(a b c))             ; => 1

;; Conditional selection
(list-take-while even? '(2 4 1 6))   ; => (2 4)
(list-drop-while even? '(2 4 1 6))   ; => (1 6)

;; Combining lists
(list-zip '(1 2) '(a b))             ; => ((1 a) (2 b))
(list-unzip '((1 a) (2 b)))          ; => ((1 2) (a b))

;; Multi-list operations
(list-map + '(1 2) '(10 20))         ; => (11 22)

;; Sorting
(list-sort < '(3 1 4 1 5))           ; => (1 1 3 4 5)

Exports

list-mapprocedure

Apply a procedure to corresponding elements of multiple lists.

R7RS-compatible multi-list map. The procedure is applied to corresponding elements from each list. Stops at the shortest list.

(list-map + '(1 2 3) '(10 20 30))         ; => (11 22 33)
(list-map list '(a b) '(1 2))             ; => ((a 1) (b 2))
(list-map + '(1 2 3) '(10 20))            ; => (11 22) - stops at shortest
list-for-eachprocedure

Apply a procedure to corresponding elements of multiple lists for side effects.

R7RS-compatible multi-list for-each. Like list-map but doesn't collect results—used for side effects. Stops at the shortest list.

(list-for-each (lambda (a b) (println "~a + ~a = ~a" a b (+ a b)))
               '(1 2 3) '(10 20 30))
list-foldprocedure

Fold over corresponding elements of multiple lists (left to right).

SRFI-1 compatible multi-list fold. The procedure receives the current elements from each list followed by the accumulator.

(list-fold + 0 '(1 2 3) '(10 20 30))  ; => 66
(list-fold (lambda (a b acc) (cons (+ a b) acc)) '() '(1 2) '(10 20))
; => (22 11)

Fold over corresponding elements of multiple lists (right to left).

Like list-fold but processes from right to left. The procedure receives elements followed by the accumulator.

(list-fold-right cons '() '(1 2 3))  ; => (1 2 3)
(list-fold-right (lambda (a b acc) (cons (list a b) acc)) '() '(1 2) '(10 20))
; => ((1 10) (2 20))
list-sortprocedure

Sort a list using a comparison procedure.

Uses stable merge sort (O(n log n)). The comparison procedure should return #t if its first argument should come before the second. Follows SRFI-132 naming convention.

(list-sort < '(3 1 4 1 5))                          ; => (1 1 3 4 5)
(list-sort string<? '("banana" "apple" "cherry"))   ; => ("apple" "banana" "cherry")
(list-sort (lambda (x y) (< (cadr x) (cadr y))) '((b 2) (a 1) (c 3)))
; => ((a 1) (b 2) (c 3))
list-any?procedure

Check if any element satisfies a predicate.

Returns #t if the predicate returns true for at least one element, #f otherwise. Returns #f for an empty list.

(list-any? even? '(1 3 4 5))   ; => #t
(list-any? even? '(1 3 5))     ; => #f
(list-any? even? '())          ; => #f
list-all?procedure

Check if all elements satisfy a predicate.

Returns #t if the predicate returns true for every element, #f otherwise. Returns #t for an empty list.

(list-all? even? '(2 4 6))     ; => #t
(list-all? even? '(2 3 4))     ; => #f
(list-all? even? '())          ; => #t
list-findprocedure

Find the first element satisfying a predicate.

Returns the first element for which the predicate returns true, or #f if no element matches.

(list-find even? '(1 3 4 5))   ; => 4
(list-find even? '(1 3 5))     ; => #f

Find the index of the first element satisfying a predicate.

Returns the zero-based index of the first matching element, or #f if no element matches.

(list-find-index even? '(1 3 4 5))   ; => 2
(list-find-index even? '(1 3 5))     ; => #f
list-indexprocedure

Find the index of the first occurrence of a value.

Returns the zero-based index of the first element equal to value, or #f if not found. Uses equal? for comparison.

(list-index 'b '(a b c))   ; => 1
(list-index 'x '(a b c))   ; => #f

Take elements from the front while a predicate is true.

Returns a list of leading elements for which the predicate returns true. Stops at the first element that fails the predicate.

(list-take-while even? '(2 4 6 1 8))   ; => (2 4 6)
(list-take-while even? '(1 2 3))       ; => ()
(list-take-while even? '())            ; => ()

Drop elements from the front while a predicate is true.

Returns the tail of the list starting at the first element that fails the predicate.

(list-drop-while even? '(2 4 6 1 8))   ; => (1 8)
(list-drop-while even? '(1 2 3))       ; => (1 2 3)
(list-drop-while even? '())            ; => ()
list-filterprocedure

Filter elements that satisfy a predicate.

Returns a new list containing only elements for which the predicate returns true. Preserves order.

(list-filter even? '(1 2 3 4 5))   ; => (2 4)
(list-filter even? '(1 3 5))       ; => ()
list-removeprocedure

Remove elements that satisfy a predicate.

Returns a new list with matching elements removed. Opposite of list-filter.

(list-remove even? '(1 2 3 4 5))   ; => (1 3 5)
(list-remove even? '(2 4 6))       ; => ()
list-zipprocedure

Combine multiple lists into a list of tuples.

Takes elements at corresponding positions from each list and groups them into sublists. Stops at the shortest list.

(list-zip '(1 2 3) '(a b c))           ; => ((1 a) (2 b) (3 c))
(list-zip '(1 2) '(a b) '(x y))        ; => ((1 a x) (2 b y))
(list-zip '(1 2 3) '(a b))             ; => ((1 a) (2 b))
list-unzipprocedure

Split a list of tuples into multiple lists.

The inverse of list-zip. Takes a list of sublists and returns multiple lists, one for each position in the sublists.

(list-unzip '((1 a) (2 b) (3 c)))      ; => ((1 2 3) (a b c))
(list-unzip '((1 a x) (2 b y)))        ; => ((1 2) (a b) (x y))
(list-unzip '())                       ; => ()