(util list) - Extra list utility library

Library (util list)

This library provides extra list utility procedures.

Function for-each-with-index proc list1 list2 ...
Function map-with-index proc list1 list2 ...

Like for-each and map, expect proc receives the index as the first argument.

(map-with-index list '(a b c) '(e f g))
((0 a e) (1 b f) (2 c g))

Function intersperse item list

Inserts item between elements in the list.

(intersperse '+ '(1 2 3))
(1 + 2 + 3)

(intersperse '+ '(1))
(1)

(intersperse '+ '())
()

Function slices list k :optional fill? padding

Splits list into the sublists (slices) where the length of each slice is k. If the length of list is not multiple of k, the last slice is dealt in the same way as take*; this is, it is shorter than k by default, or added padding if fill? is true.

(slices '(a b c d e f g) 3)
((a b c) (d e f) (g))

(slices '(a b c d e f g) 3 #t 'z)
((a b c) (d e f) (g z z))

Function split-at* list k :optional (fill? #t) (padding #f)

Splits the list list at index k. This is more tolerant version of split-at defined in SRFI-1 library. Returns the results of take* and drop*.

(split-at* '(a b c d) 6 #t 'z)
(a b c d z z) and ()

Function take* list k :optional (fill? #f) (padding #f)
Function drop* list k

More tolerant version of take and drop defined in SRFI-1 library. These won't raise an error when k is larger than the size of the given list.

If the list is shorter than k elements, take* returns a copy of list by default. If fill? is true, padding is added to the result to make its length k.

On the other hand, drop* just returns as empty list when the input list is shorter than k elements.

(take* '(a b c d) 3)
(a b c)

(take* '(a b c d) 6)
(a b c d)

(take* '(a b c d) 6 #t)
(a b c d #f #f)

(take* '(a b c d) 6 #t 'z)
(a b c d z z)

(drop* '(a b c d) 3)
(d)

(drop* '(a b c d) 5)
()

Macro cond-list clause ...

Construct a list by conditionally adding entries. Each clause must have a test and expressions. When its test yields true, then result of associated expression is used to construct the resulting list. When the test yields false, nothing is inserted.

Clause must either one of the following form:

(test expr ...)

Test is evaluated, and when it is true, expr ... are evaluated, and the return value becomes a part of the result. If no expr is given, the result of test is used if it is not false.

(test => proc)

Test is evaluated, and if it is true, proc is called with the value, and the return value is used to construct the result

(test expr ...)

Like (test expr ...), except that the result of the last expr must be a list, and it is spliced into the resulting list, like unquote-splicing.

(test => proc)

Like (test => proc), except that the result of the last proc must be a list, and it is spliced into the resulting list, like unquote-splicing.

(let ((alist '((x 3) (y -1) (z 6))))
 (cond-list ((assoc 'x alist) 'have-x)
            ((assoc 'w alist) 'have-w)
            ((assoc 'z alist) => cadr)))
(have-x 6)

(let ((x 2) (y #f) (z 5))
  (cond-list (x   `(:x ,x))
             (y   `(:y ,y))
             (z   `(:z ,z))))
(:x 2 :z 5)