18 List utilities
18.1 Regular lists
Example: |
> (make-list* 10 '(1 2 3)) |
(1 2 3 1 2 3 1 2 3 1) |
Examples: | ||||
| ||||
| ||||
(100 1 300 3) |
Example: | |||
| |||
((0 1) (1 2) (2 3) (3 1) (4 2) (5 3) (6 1) (7 2) (8 3) (9 1)) |
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) |
Example: |
> (list-delimit '(1 2 3 4 5) ", ") |
(1 ", " 2 ", " 3 ", " 4 ", " 5) |
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 |
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) |
Examples: |
> (list-ref? '() 0) |
#f |
> (list-ref? '(1 2 3) 0) |
#t |
> (list-ref? '(1 2 3) 3) |
#f |
| ||||||||||
a : list? | ||||||||||
b : list? | ||||||||||
same? : (any any -> boolean?) = equal? |
members of a that aren’t in b;
members of b that aren’t in a;
members that are in a and b.
Example: | |||||
| |||||
((2 11) (9) (1 3 5 7)) |
| ||||||||||||||||||||||||||||
list1 : list? | ||||||||||||||||||||||||||||
list2 : list? | ||||||||||||||||||||||||||||
same? : (any any -> boolean?) | ||||||||||||||||||||||||||||
less-than? : (any any -> boolean?) |
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) | ||
| ||
("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 |
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) |
Unlike the SRFI 1 procedures unzip2, unzip3 and so on, this procedure returns multiple values (rather than a list of values).
Example: | ||||
| ||||
(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?) |
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 |
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? |
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?) |
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)) |
Example: |
> (alist-map + '((1 . 2) (3 . 4) (5 . 6))) |
(3 7 11) |
Examples: | ||
| ||
> (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 |
Examples: | |||
| |||
((a . 1) (b . 2) (c . 3) (d . 40)) | |||
| |||
((a . 1) (b . 20) (c . 30) (d . 40)) |