On this page:
18.1 Regular lists
make-list*
assemble-list
in-list/ cycle
list-swap
list-delimit
list-pad
list-pad-right
list-ref?
list-diff
merge-sorted-lists
char-iota
unzip-values
18.2 Association lists
assoc-value
assoc-value/ default
alist-set
alist-delete
alist-map
alist-for-each
alist-merge

18 List utilities

 (require (planet untyped/unlib/list))
Utilities for lists.

18.1 Regular lists

(make-list* num items)  list?
  num : natural?
  items : (cons any (listof any))
Like make-list from SRFI 1 but capable of building a list from cycles of several items.

Example:

  > (make-list* 10 '(1 2 3))

  (1 2 3 1 2 3 1 2 3 1)

(assemble-list clause ...)
 
clause = [expression item ...]
Syntax for accumulating a list of items. Each clause contains a boolean expression and zero or more items: the items are added to the list if and only if the expression is non-#f:

Examples:

  > (define-values (x y z)
      (values 1 2 3))
  > (assemble-list
     [(odd? x) 100 x]
     [(odd? y) 200 y]
     [(odd? x) 300 z])

  (100 1 300 3)

(in-list/cycle items)  sequence?
  items : (cons any (listof any))
Like in-list but creates a sequence that cycles through items indefinitely.

Example:

  > (for/list ([i (in-range 0 10)]
               [j (in-list/cycle '(1 2 3))])
              (list i j))

  ((0 1) (1 2) (2 3) (3 1) (4 2) (5 3) (6 1) (7 2) (8 3) (9 1))

(list-swap lis index1 index2)  list?
  lis : list?
  index1 : natural?
  index2 : natural?
Returns a copy of lis with the values at index1 and index2 swapped over.

Examples:

  > (list-swap '(1 2 3 4 5) 1 3)

  (1 4 3 2 5)

  > (list-swap '(1 2 3 4 5) 1 5)

  list-ref: index 5 too large for list: (1 2 3 4 5)

(list-delimit lis delim)  list?
  lis : list?
  delim : any
Inserts delim between each pair of consecutive items in lis. Useful for assembling x-expressions in web applications.

Example:

  > (list-delimit '(1 2 3 4 5) ", ")

  (1 ", " 2 ", " 3 ", " 4 ", " 5)

(list-pad lis target-length [item])  list?
  lis : list?
  target-length : natural?
  item : any = #f
Creates a new list by repeatedly adds item to the left of lis until the target-length is reached. lis is returned if it is already target-length or longer.

Examples:

  > (list-pad '(1 2 3) 5)

  (#f #f 1 2 3)

  > (list-pad '(1 2 3) 5 #t)

  (#t #t 1 2 3)

  > (list-pad '(1 2 3 4 5) 3)

  (1 2 3 4 5)

(list-pad-right lis target-length [item])  list?
  lis : list?
  target-length : natural?
  item : any = #f
Creates a new list by repeatedly adds item to the right of lis until the target-length is reached. lis is returned if it is already target-length or longer.

Examples:

  > (list-pad-right '(1 2 3) 5)

  (1 2 3 #f #f)

  > (list-pad-right '(1 2 3) 5 #t)

  (1 2 3 #t #t)

  > (list-pad-right '(1 2 3 4 5) 3)

  (1 2 3 4 5)

(list-ref? lis n)  boolean?
  lis : list?
  n : natural?
Returns #t if lis contains n or more items.

Examples:

  > (list-ref? '() 0)

  #f

  > (list-ref? '(1 2 3) 0)

  #t

  > (list-ref? '(1 2 3) 3)

  #f

(list-diff a b [same?])  
list? list? list?
  a : list?
  b : list?
  same? : (any any -> boolean?) = equal?
Compares members of a and b using same? and returns three lists:

Example:

  > (call-with-values
     (lambda ()
       (list-diff (list 1 2 3 5 7 11)
                  (list 1 3 5 7 9)))
     list)

  ((2 11) (9) (1 3 5 7))

(merge-sorted-lists list1    
  list2    
  same?    
  less-than?)  list?
  list1 : list?
  list2 : list?
  same? : (any any -> boolean?)
  less-than? : (any any -> boolean?)
Merges list1 and list2 in O(n) time. The result is a sorted list of items from both lists, with all duplicates removed.

Duplicates are detected using the supplied predicate same?. Items are taken from list1 when duplicates are detected.

The procedure assumes list1 and list2 are sorted in ascending order according to the supplied predicate less-than?. More formally, for each pair of consecutive items a and b in either list, (less-than? a b) must be #t.

Examples:

  > (merge-sorted-lists '(1 3 5) '(2 4) = <)

  (1 2 3 4 5)

  > (merge-sorted-lists '("a" "b" "c") '("b" "c" "d")
                        string=? string<?)

  ("a" "b" "c" "d")

  > (merge-sorted-lists '("a" "b" "c") '("b" "c" "d") eq? string<?)

  ("a" "b" "b" "c" "c" "d")

  ; Sorted in the wrong order: results undefined:
  > (merge-sorted-lists '(1 3 5) '(4 2) = <)

  (1 3 4 2 5)

(char-iota count [start step])  (listof char?)
  count : integer?
  start : char? = #\a
  step : positive-integer? = 1
The character equivalent of iota from SRFI 1.

Examples:

  > (char-iota 5)

  (#\a #\b #\c #\d #\e)

  > (char-iota 5 #\V)

  (#\V #\W #\X #\Y #\Z)

  > (char-iota 5 #\a 2)

  (#\a #\c #\e #\g #\i)

(unzip-values seq)  
list? ...
  seq : (sequenceof list?)
"Unzips" a sequence of lists into several lists, one for the cars of each item, one for the cadrs, and so on.

Unlike the SRFI 1 procedures unzip2, unzip3 and so on, this procedure returns multiple values (rather than a list of values).

Example:

  > (unzip-values '((1 2 3)
                    (4 5 6)
                    (7 8 9)
                    (10 11 12)))

  (1 4 7 10)

  (2 5 8 11)

  (3 6 9 12)

18.2 Association lists

(assoc-value key alist)  any
  key : any
  alist : (listof pair?)
Like assoc but returns the cdr of the relevant pair. Raises exn:fail if key is not found.

Examples:

  > (assoc-value 'a '((a . 1) (b . 2)))

  1

  > (assoc-value 'b '((a . 1) (b . 2)))

  2

  > (assoc-value 'c '((a . 1) (b . 2)))

  assoc-value: key not found: c ((a . 1) (b . 2))

(assoc-value/default key alist default)  any
  key : any
  alist : (listof pair?)
  default : any
Like assoc but returns the cdr of the relevant pair. Returns default if key is not found.

Examples:

  > (assoc-value/default 'a '((a . 1) (b . 2)) #f)

  1

  > (assoc-value/default 'b '((a . 1) (b . 2)) #f)

  2

  > (assoc-value/default 'c '((a . 1) (b . 2)) #f)

  #f

(alist-set key val alist [same?])  (alistof a b)
  key : a
  val : b
  alist : (alistof a b)
  same? : (a b -> boolean) = equal?
Returns a copy of alist with the value mapped to key replaced with val. If key does not appear in alist, a new pair is added to the end. The order of existing keys in alist is preserved. The optional same? argument is used to check for key equality.

Examples:

  > (alist-set 'a 5 '((a . 1) (b . 2)))

  ((a . 5) (b . 2))

  > (alist-set 'c 5 '((a . 1) (b . 2)))

  ((a . 1) (b . 2) (c . 5))

(alist-delete key alist)  (listof pair?)
  key : any
  alist : (listof pair?)
Reprovided from SRFI 1. Returns a copy of alist with key removed.

Examples:

  > (alist-delete 'a '((a . 1) (b . 2)))

  ((b . 2))

  > (alist-delete 'c '((a . 1) (b . 2)))

  ((a . 1) (b . 2))

(alist-map proc alist)  (listof ans)
  proc : (key val -> ans)
  alist : (listof (cons key val))
Maps proc over alist, returning a list of the results.

Example:

  > (alist-map + '((1 . 2) (3 . 4) (5 . 6)))

  (3 7 11)

(alist-for-each proc alist)  void?
  proc : (key val -> void?)
  alist : (listof (cons key val))
Applies proc to each item in alist for its side effects.

Examples:

  > (define (print-pair key val)
      (printf "~a = ~a, " key val))
  > (alist-for-each print-pair '((1 . 2) (3 . 4) (5 . 6)))

  1 = 2, 3 = 4, 5 = 6,

(alist-merge alist1 alist2 [prefer])  (alistof pair?)
  alist1 : (listof pair?)
  alist2 : (listof pair?)
  prefer : (U 'first 'second) = 'first
Merges alist1 and alist2, merging colliding keys together. prefer determines which alist the value is taken from if keys collide. equal? is used as a key comparison function. The order of keys is preserved where possible.

Examples:

  > (alist-merge '((a . 1) (b . 2) (c . 3))
                 '((b . 20) (c . 30) (d . 40)))

  ((a . 1) (b . 2) (c . 3) (d . 40))

  > (alist-merge '((a . 1) (b . 2) (c . 3))
                 '((b . 20) (c . 30) (d . 40))
                 'second)

  ((a . 1) (b . 20) (c . 30) (d . 40))