19List utilities

19.1Regular lists

Like make-list from SRFI 1 but capable of building a list from cycles of several items.

Example:

> (make-list*10'(123))

(1231231231)

(assemble-listclause...)

clause

=

[expressionitem...]

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

(values123))

> (assemble-list

[(odd?x)100x]

[(odd?y)200y]

[(odd?x)300z])

(10013003)

(in-list/cycleitems)→sequence?

items:(consany(listofany))

Like in-list but creates a sequence that cycles through items indefinitely.

Example:

> (for/list([i(in-range010)]

[j(in-list/cycle'(123))])

(listij))

((01)(12)(23)(31)(42)(53)(61)(72)(83)(91))

(list-swaplisindex1index2)→list?

lis:list?

index1:natural?

index2:natural?

Returns a copy of lis with the values at index1 and index2 swapped over.

Examples:

> (list-swap'(12345)13)

(14325)

> (list-swap'(12345)15)

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

(list-delimitlisdelim)→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'(12345)", ")

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

(list-padlistarget-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'(123)5)

(#f#f123)

> (list-pad'(123)5#t)

(#t#t123)

> (list-pad'(12345)3)

(12345)

(list-pad-rightlistarget-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'(123)5)

(123#f#f)

> (list-pad-right'(123)5#t)

(123#t#t)

> (list-pad-right'(12345)3)

(12345)

(list-ref?lisn)→boolean?

lis:list?

n:natural?

Returns #t if lis contains n or more items.

Examples:

> (list-ref?'()0)

#f

> (list-ref?'(123)0)

#t

> (list-ref?'(123)3)

#f

(list-diffab[same?])

→

list?

list?

list?

a:list?

b:list?

same?:(anyany->boolean?)=equal?

Compares members of a and b using same? and returns three lists:

members of a that aren’t in b;

members of b that aren’t in a;

members that are in a and b.

Example:

> (call-with-values

(lambda()

(list-diff(list1235711)

(list13579)))

list)

((211)(9)(1357))

(merge-sorted-lists

list1

list2

same?

less-than?)

→

list?

list1:list?

list2:list?

same?:(anyany->boolean?)

less-than?:(anyany->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?ab) must be #t.

Examples:

> (merge-sorted-lists'(135)'(24)=<)

(12345)

> (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'(135)'(42)=<)

(13425)

(char-iotacount[startstep])→(listofchar?)

count:integer?

start:char?=#\a

step:positive-integer?=1

The character equivalent of iota from SRFI 1.

Examples:

> (char-iota5)

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

> (char-iota5#\V)

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

> (char-iota5#\a2)

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

(unzip-valuesseq)

→

list?

...

seq:(sequenceoflist?)

"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'((123)

(456)

(789)

(101112)))

(14710)

(25811)

(36912)

19.2Association lists

(assoc-valuekeyalist)→any

key:any

alist:(listofpair?)

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/defaultkeyalistdefault)→any

key:any

alist:(listofpair?)

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-setkeyvalalist[same?])→(alistofab)

key:a

val:b

alist:(alistofab)

same?:(ab->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'a5'((a. 1)(b. 2)))

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

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

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

(alist-deletekeyalist)→(listofpair?)

key:any

alist:(listofpair?)

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-mapprocalist)→(listofans)

proc:(keyval->ans)

alist:(listof(conskeyval))

Maps proc over alist, returning a list of the results.

Example:

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

(3711)

(alist-for-eachprocalist)→void?

proc:(keyval->void?)

alist:(listof(conskeyval))

Applies proc to each item in alist for its side effects.

Examples:

> (define(print-pairkeyval)

(printf"~a = ~a, "keyval))

> (alist-for-eachprint-pair'((1. 2)(3. 4)(5. 6)))

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

(alist-mergealist1alist2[prefer])→(alistofpair?)

alist1:(listofpair?)

alist2:(listofpair?)

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.