1 Exception utilities
2 Number utilities
3 String utilities
4 Bytes utilities
5 Symbol utilities
6 List utilities
7 PLT 4x hash utilities
8 PLT 3x hash utilities
9 URL utilities
10 Contract utilities
11 File and path utilities
12 Parameter utilities
13 Syntax utilities
14 SRFI19 time utilities
15 Scribble utilities
16 Generators
17 Generators (short names)
18 Pipelines
19 Write-through cache
20 Yieldable procedures
21 Debugging tools
22 Profiling tools
23 Logging tools
On this page:
6.1 Regular lists
make-list*
assemble-list
in-list/ cycle
list-swap
list-delimit
list-pad
list-pad-right
merge-sorted-lists
char-iota
6.2 Association lists
assoc-value
assoc-value/ default
alist-set
alist-delete
alist-map
alist-for-each
alist-merge
Version: 4.0.1.2

 

6 List utilities

 (require (planet untyped/unlib/list))

Utilities for lists.

6.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.

Examples:

  > (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.

Examples:

  > (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.

Examples:

  > (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)

(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)

6.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.

Examples:

  > (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))