6 List utilities
Utilities for lists.
6.1 Regular lists
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) |
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) |
| ||||||||||||||||||||||||||||
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) | ||
| ||
("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) |
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 |
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: |
(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: | ||
| ||
> (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: | |||
| |||
((a . 1) (b . 2) (c . 3) (d . 40)) | |||
| |||
((a . 1) (b . 20) (c . 30) (d . 40)) |