1 Exception utilities
2 Number utilities
3 String utilities
4 Symbol utilities
5 SRFI19 time utilities
6 List
7 PLT 4x hash utilities
8 PLT 3x hash utilities
9 Contract utilities
10 File and path utilities
11 Parameter utilities
12 Syntax utilities
13 Generators
14 Generators (short names)
15 Pipelines
16 Write-through cache
17 Yieldable procedures
18 Debugging tools
19 Profiling tools
20 Logging tools
On this page:
6.1 Regular lists
list
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: 3.99.0.23

 

6 List

 (require (planet untyped/unlib/list))

Utilities for lists.

6.1 Regular lists

(list 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)  (listof pair?)

  key : any

  val : any

  alist : (listof pair?)

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.

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