2023-04-05

(sph list)

list helpers.

part of sph-lib

library description

currently also contains bindings for non-list pairs.

# syntax

## let*-list

like let*, but variable names enclosed in round brackets bind list elements.

binding is done like lambda and apply.

examples

  (let*-list ((a 1) ((b c) (list 2 3))) (and (= 1 a) (= 2 b) (= 3 c)))

  (let*-list (((a b . c) (list 1 2 3 4))) #t)

module name

(sph list)

exported bindings

procedure: any->list a ->
any -> list
wraps a non-list argument in a list
procedure: compact a ->
list -> list
keep only true elements in list. removes all boolean false values
procedure: complement lists ... ->
list ... -> list
delete elements from the first list that are included in the other lists
procedure: complement-both a b ->
list list -> (list list)
delete elements in both lists that are included in both lists
procedure: consecutive f a [c] ->
procedure:{any -> any/boolean} list [procedure] -> (list:matches list:rest)
splits the list into two lists, the first being a list of all beginning elements of \"a\" that consecutively matched
\"f\", the second being the rest.
like srfi-1 span but the result is a list and not multiple return values
procedure: contains-all? a values ->
list ... -> boolean
test if argument \"a\" contains all of the given values
procedure: contains-some? a values ->
list ... -> boolean
test if argument \"a\" contains any of the given values
procedure: contains? a value [member] ->
list any [procedure:{any list -> boolean/any}] -> boolean
return a boolean indicating if list \"a\" contains \"value\"
procedure: containsq? a value ->
true if list contains value.
comparison with eq?
procedure: containsv-some? a values ->
list list -> boolean
test if argument \"a\" contains any of the given values
procedure: containsv? a value ->
true if list contains value.
comparison with eqv?
procedure: convolve a b ->
procedure: count-value value a [equal?] ->
any list -> integer
count occurences of \"value\" in list
procedure: count-value-with-limit value a [count-limit member] ->
any list [integer procedure:{any list -> boolean/any}] -> integer
like count-value but with an optional parameter for a count at which to stop counting
procedure: count-with-limit pred limit a ... ->
procedure integer list ... -> integer
like \"count\" but with an optional parameter for a count at which to stop counting
syntax: define-list name a ...
procedure: delete-duplicates-sorted a [equal-f preserve-order] ->
list [procedure:{any any -> boolean} boolean] -> list
delete duplicates from a sorted list using a more efficient algorithm than for unsorted lists
procedure: difference lists ... ->
list ... -> list
result in a list of elements not included in all given lists
procedure: difference-and-intersection lists ... ->
list ... -> (list list)
results in a list with two elements, one being the symmetric-difference between the given lists and one being the intersection.
that means one list of all elements that are included in all lists, and one list of elements that are not included in all lists.
it does both calculations in one step saving resources compared to making them in separate steps.
procedure: difference-and-intersection-p equal-f lists ... ->
{any any -> boolean} list ... -> (list:difference list:intersection)
like difference+intersection but the predicate for comparing list elements can be specified
procedure: difference-p equal-f lists ... ->
{any any -> boolean} list ... -> list
like
procedure: drop* count a ->
like srfi-1 drop but with reversed argument order (like stream-drop from srfi-41) and
returns null if list contains less elements than count instead of raising an exception
procedure: duplicates a ->
get a list of distinct values that occur more than once in the same list
procedure: each-first-middle-last first-f middle-f last-f a ... ->
procedure procedure procedure list ... ->
untested.
call \"first-f\" for the first element,
call \"last-f\" for the last element,
call \"middle-f\" for a list of all elements inbetween
procedure: each-in-index-range f start end a ... ->
procedure integer integer list ... ->
untested.
call f only for elements in index range between \"start\" and \"end\" inclusively
procedure: each-slice f slice-length a ->
procedure:{list ->} integer list ->
apply f to each slice of slice-length elements from list.
procedure: each-with-index f a ... ->
procedure:{index element ... ->} list ... ->
apply f to each element and its index in list a
procedure: every-fold f state a ... ->
procedure:{any:state any:element ... -> any:next-state} list:elements ... -> any:state/false
like fold but results in false if any result of f is not a true value
procedure: every-map f a ... ->
procedure:{any -> any} list ... -> list/false
like map but results in false if any result of f is not a true value
procedure: every-or-index f a ... ->
procedure:{any ... -> boolean} list ... -> true/integer
true if \"f\" is true for all elements, otherwise the index of the element for which \"f\" was false
procedure: false-if-null a ->
procedure: filter-append-map f lists ... ->
apply filter-map and then apply append on the result
procedure: filter-produce f a ... ->
procedure list ...
apply \"f\" to each ordered combination of elements from lists, cartesian product, and return true results in a list.
supports multiple lists and treats non-list arguments as the single element of a list.
example:
(produce f (1 2) (4 5) 6)
is equivalent to
(list (f 1 4 6) (f 1 5 6) (f 2 4 6) (f 2 5 6))
procedure: first-intersection a b ->
list list -> any
give the first found element that is included in both lists
procedure: first-intersection-p equal-f a b ->
{any any -> boolean} list list -> any
like first-intersection but the procedure for comparing elements can be specified
procedure: first-or-false a ->
list -> any/false
give the first element of a list if it is not null, otherwise false
procedure: first-or-null a ->
results in the first element of a list if it is not null, otherwise null
procedure: flat? a ->
list -> boolean
true if the list does not contain a list
procedure: flatten a ->
list -> (non-list ...)
replace sublists with their content, resulting in a list that does not contain lists
procedure: fold* f a custom-state-values ... ->
procedure:{any:list-element any:state-value ... -> (any:state-value)} list any:state-value ... -> list:state-values
like fold but with multiple state values. the state values are updated by returning a list from a call to \"f\".
apply \"f\" to each element of \"a\" and the state-value elements that were given to
fold-multiple or subsequently the updated state-values from the previous call to \"f\"
procedure: fold-c* f a custom-state-values ... ->
procedure:{any:list-element any:state-value ... -> (any:state-value)} list any:state-value ... -> list:state-values
like fold but with multiple state values. the state values are updated by returning a list from a call to \"f\".
apply \"f\" to each element of \"a\" and the state-value elements that were given to
fold-multiple or subsequently the updated state-values from the previous call to \"f\"
procedure: fold-integers count init f ->
integer any {integer any -> any} -> any
fold over integers from 0 to count - 1
procedure: fold-multiple f a custom-state-values ... ->
procedure:{any:list-element any:state-value ... -> (any:state-value)} list any:state-value ... -> list:state-values
like fold but with multiple state values. the state values are updated by returning a list from a call to \"f\".
apply \"f\" to each element of \"a\" and the state-value elements that were given to
fold-multiple or subsequently the updated state-values from the previous call to \"f\"
procedure: fold-multiple-c f a custom-state-values ... ->
procedure:{any:element procedure:continue:{list:next-pair any:state-value ...} any:state-value ... -> any} list any:state-value ... -> list
procedure: fold-multiple-right f a r ... ->
procedure list any ... -> any
like fold-multiple but works through the list elements from last to first
procedure: fold-right* f a r ... ->
procedure list any ... -> any
like fold-multiple but works through the list elements from last to first
procedure: fold-segments size f init a ->
integer {any:state element ... -> any:state} any:state list -> any
fold over each overlapping segment with length \"size\".
example:
(fold-segments f 2 #t (list 4 5 6 7))
procedure: fold-slice slice-length f init a ->
integer procedure:{any:state any:element ... -> any} any list -> any:state
call f with each slice-length number of consecutive elements of a
procedure: fold-span filter-f f a ->
procedure:{any -> any/false} procedure:{list -> any} list -> any
fold over each list of elements that consecutively matched filter-f (utilising the \"span\" procedure)
procedure: fold-unless f stop? default init a ... ->
{any ... -> any} {any -> boolean/any} any any list ... -> any
like fold, but returns \"default\" if \"stop?\" is true
procedure: fold-unless-check-init f stop? default init a ... ->
procedure: fold-until f init stop? a ->
procedure any procedure:{any -> boolean} list -> any
end folding if \"stop?\" is true for a result and return the result
procedure: group-consecutive filter-f a ->
{any -> boolean} list -> list
wrap multiple elements that consecutively match \"filter-f\" in a list
procedure: group-split-at-matches start-group? a ->
procedure:{any -> boolean} list -> (list ...)
wrap consecutive elements in lists. elements for which \"start-group?\" is true become the first element of a new list.
example
(group-split-at-matches integer? (list \"a\" \"b\" 1 \"c\" \"d\" 2 \"e\"))
->
((\"a\" \"b\") (1 \"c\" \"d\") (2 \"e\"))
procedure: improper-list-split-at-last a ->
pair:improper-list -> (list any:non-pair)
(1 2 . 3) -> ((1 2) 3)
procedure: insert-second a b ->
any list -> list
insert \"a\" as the second element into list \"b\"
procedure: interleave a value ->
list any -> list
inserts value in front of each element in \"a\" except the first element.
example: (interleave (list 1 2 3) 4) -> (1 4 2 4 3)
procedure: intersection lists ... ->
list ... -> list
result in a list of all elements which are contained in all given lists
procedure: intersection-p equal-f rest ... ->
procedure:{any any -> boolean} list ... -> list
like \"intersection\" but the predicate for comparing the list elements can be specified
procedure: iterate-three f a states ... ->
procedure:{list:prev any:current list:next any:state ... -> any:state ...} list any:state-init ... -> list:state
calls \"f\" for each list element, previous list elements and following list elements.
multiple custom values can be updated each call with the result of \"f\" which must be a list
procedure: iterate-three-stop-end stop? end map-f a states ... ->
{list any list any ... -> boolean} {list any list any ... -> list:state-values}:after-stop? {list any list any ... -> list:state-values} list any ... -> any
like \"iterate-three\" but takes two additional procedures - one for stopping the iteration
after a \"map-f\" result, and one that is called for the last element or when \"stop?\" is true
syntax: let*-list (((name ...) a) rest ...) body ...
syntax: list-bind a lambda-formals body ...
bind elements of list \"a\" to \"lambda-formals\"
procedure: list-deselect a indices ->
list (integer ...) -> list
return a new, eventually smaller, list consisting of values not at specified indices
procedure: list-distribute a indices default ->
list (integer ...) any -> list
creates a new list with values from list a at positions indices. the value for \"no-element\" is set at indices
not included in the list indices. the length of indices must equal the length of a, and indices should not have duplicates.
procedure: list-distribute-sorted a indices default ->
like list-distribute but faster. works only correctly for indices lists that are sorted ascending
procedure: list-index-value a value [equal-f] ->
get the index of value in list
procedure: list-indices f a ->
procedure:{any -> boolean} list -> (integer ...)
create a list of all indices for which f results in true
procedure: list-logical-condition? a ->
any -> boolean
true if \"a\" is a list-logical condition
procedure: list-logical-contains? a condition ->
list list -> boolean
test for value inclusion with a condition list like ([or/and/not] value/condition ...).
example:
(list-logical-contains? (list 1 2 3) (quote (and 2 3 (or 4 1 5) (not 8)))) -> #t
procedure: list-logical-match match-one? condition ->
procedure:{any -> boolean} list -> false/any:last-sub-condition-result
match a logical condition that is a possibly nested list with and/or/not symbol prefixes.
match-one? is called for each element in condition that is not a condition prefix.
returns false early if a required part of the condition does not match.
condition: ([symbol:and/or/not] any/condition ...)
example
(list-logical-match (l (b) (contains? somelist b)) (q (and 1 2 (or (and 3 4) (and 5 6)))))
procedure: list-page a entry-count number lookahead c ->
list integer integer integer procedure:{list boolean:last-page? -> any} -> any
pass a list of \"entry-count\" elements at an offset of (* number entry-count),
eventually including \"lookahead\" number of elements if they are the last elements
and a boolean indicating if it is the last page to continuation procedure \"c\"
procedure: list-prefix? a prefix ->
list list -> boolean
true if the given \"prefix\" elements exist in order at the beginning of list.
examples:
(list-prefix? (list 3 2 4) (list 3 1)) -> #f
(list-prefix? (list 3 2 4) (list 3 2)) -> #t
procedure: list-replace-last a replacement ->
list any/procedure:{any -> any} -> list
replace the last element in a list
procedure: list-replace-last-n n a replacement ->
list integer any/procedure:{any ... -> any/list} -> list
procedure: list-select a indices ->
list (integer ...) -> list
return a new list consisting of values at indices
procedure: list-set-add = list rest ... ->
Add to LIST any of the elements of REST not already in the list.
These elements are `cons'ed onto the start of LIST (so the return shares
a common tail with LIST), but the order they're added is unspecified.

The given `=' procedure is used for comparing elements, called
as `(@var{=} listelem elem)', i.e., the second argument is one of the
given REST parameters.
procedure: list-set-difference = list1 rest ... ->
procedure: list-set-equal? a ... ->
list ... -> boolean
true if all elements of the given lists appear in all others.
uses \"equal?\" for element equality comparison
procedure: list-set-eqv? a ... ->
list ... -> boolean
like \"list-set-equal?\" but uses \"eqv?\" for element equality comparison
procedure: list-set-subset? = rest ... ->
procedure: list-set-union = rest ... ->
procedure: list-sort-by-list order a [accessor] ->
list list -> list
sort a list so the elements correspond to the order of elements in list \"order\".
elements not contained in \"order\" are moved to the end of the result list.
examples:
(list-sort-by-list (list 3 2 4) (list 4 2 3)) -> (3 2 4)
(list-sort-by-list (list 3 2 4) (list 4 5 2 3)) -> (3 2 4 5)
procedure: list-sort-with-accessor less? accessor a ->
procedure:{any any -> boolean} procedure:{any:list-element -> any} list -> list
sort list by calling accessor for each argument before comparison. only the order of elements changes, the individual elements are not changed
procedure: list-suffix? a suffix ->
list list -> boolean
true if the given \"suffix\" elements exist in order at the end of list.
see also \"list-prefix?\"
procedure: list-tail-ref a b ->
procedure: map-apply f a ... ->
procedure:{any ... -> any} (list ...) ... -> list
like map but the procedure is applied with elements of \"a\" as arguments.
instead of calling f like (f (list 1 2)) like \"map\" would do, f is called like (f 1 2)
example
(map-apply f (list (list 1 2) (list 3 4)))
procedure: map-c f lists ... ->
procedure:{procedure:{any:new-element -> any}:continue any:element ... -> any:last-result} list ... -> list
map over list with a procedure that when called with the current map result continues the mapping.
if the procedure is not called, the result of the current call will become the tail of the result list.
maps only the length of the shortest list if multiple lists are given
example
(map-c (l (c a) (if (> 3 a) (c (+ 1 a)) (list))) (list 1 2 3 4 5))
->
(2 3 4)
procedure: map-consecutive filter-f f a ->
{any -> boolean} {any any ... -> any} list -> list
\"f\" is called for and with every list of elements that consecutively matched \"filter-f\". at least two elements at a time
procedure: map-first f a ->
procedure list -> list
call \"f\" for the first element of list and replace the first element in the list with the result of \"f\".
replace-first
procedure: map-fold f a init ... ->
procedure list any ... -> list any ...
procedure:{(list-element state ...) -> (list-element state ...)}
combination of map and fold.
call f with each list element and state values, which are set to init for the first call.
each call to f must return a list of: the mapped result element and one
element for each updated value of state.
example: (map-fold (l (a index) (list (+ a index) (+ 1 index))) (list 1 2 3) 0)
procedure: map-integers count f ->
integer procedure:{integer -> any} -> list
map over integers from 0 to count - 1
procedure: map-last-n n b f ->
procedure:{any ... -> any/(any ...)} list -> list
call f to replace the last n elements in list b.
if the result of f is a list it is spliced so that the
elements can be replaced with multiple elements
procedure: map-map f a ... ->
procedure (list ...) ... -> list
given a list of lists, maps over the elements of lists.
like (map (l (a) (map f a) a))
procedure: map-one predicate f a ->
{any -> any}:predicate {any:element -> any} list -> list
apply f only to the first element that matches predicate.
all elements that do not match are mapped with the \"identity\" function
procedure: map-segments size f a ->
integer procedure:{any ... -> any} list -> list
map over each overlapping segment of length len.
each segment is one step apart.
example: for (1 2 3 4) size 2 maps (1 2) (2 3) (3 4)
procedure: map-selected select? f a ... ->
procedure procedure list ... -> list
apply f only to elements for which \"select?\" is true. unmatched items are included in the result list.
if multiple lists are given, it works like \"map\" except that the elements from the multiple lists for one call that are not selected are saved as a list.
map-some/map-only
procedure: map-slice slice-length f a ->
integer procedure:{any ... -> any} list -> list
call \"f\" with each \"slice-length\" number of consecutive elements of \"a\"
procedure: map-span filter-f f a ->
procedure:{any -> any/false} procedure:{any any ... -> any} list -> list
apply \"f\" to each list of elements that consecutively matched \"filter-f\".
an unpredictable number of arguments might be passed to f. with (lambda a body ...) a single list can still be accessed.
this allows for things like (map-span string? string-append a)
procedure: map-unless f stop? default a ... ->
procedure stop? list -> list/boolean:false
{any -> any} {any -> boolean} list -> list/boolean
map unless \"stop?\" is true for a mapping-result. return an empty list or \"default\" if \"stop?\" was true
procedure: map-with-index f a ... ->
procedure:{integer:index any:element ... -> any} list ... -> list
procedure: pair->list a ->
pair -> list
syntax: pair-bind a (b c) body ...
bind the first and second value of \"a\" to \"b\" and \"c\" respectively.
ideally, maybe, lambda/apply should support (apply (lambda (a . b)) (pair 1 2))
procedure: pair-fold-multiple f a init ... ->
{pair any -> any} list any ... -> any
like fold-multiple but calling f with the pairs of list
procedure: pair-map f a ->
procedure list -> list
like map but not the list elements are passed to \"f\" but the pairs of the list.
for example (1 2 3) is just another notation for the pair notation (1 . (2 . (3 . ())))
instead of mapping (1 2 3) pair-map maps ((1 2 3) (2 3) (3))
procedure: pair-reverse a ->
pair -> pair
reverse the order of values in a pair.
example: (pair-reverse (pair 1 2)) -> (2 . 1)
procedure: pattern-match-min-length a ->
list -> integer
takes a flat list with symbols and ellipses and counts the required parts of a pattern with
symbols interpreted as matching any element and ellipses to match zero or many occurences of the previous element.
# examples
((a ...)) -> 0
((a a ...)) -> 1
((a ... b ...)) -> 0
((a ... b ... c d)) -> 2
procedure: produce f a ... ->
procedure: produce-unless f stop? default a b ->
{any any -> any} {any -> boolean} any list list -> false/any
produce two lists unless \"stop?\" is true for a production-result. if stop? is true, result in false
procedure: replace a select? replacement ->
list procedure any -> list
procedure: replace-at-once match? f a ->
procedure:{any -> boolean} procedure:{list:matched-elements -> list:replacements} list:source -> list
all elements matching \"match?\" are collected in a list and passed to \"f\".
the result of \"f\" is then used to replace the matched elements in source in order
procedure: replace-value a search-value replacement [equal-f] ->
list any any [procedure:{any any -> boolean}] -> list
procedure: simplify a ->
any/list -> list/pair/any
list with one element -> element
list with two non-pair elements -> pair
procedure: simplify-list a ->
list -> list
examples:
(((1 2))) -> (1 2)
(((1 2) (3))) -> ((1 2) (3))
removes extra nesting
variable: sph-list-description
procedure: splice predicate a ->
{list -> boolean} list -> list
splice elements that are lists and match predicate
procedure: splice-last-list a ->
list -> list
if the last element is a list, append it to the previous elements.
example: (splice-last-list (1 2 (3 4))) -> (1 2 3 4)
procedure: split-at-last a ->
list -> (list list)
get a list with the list of the initial elements and a list with the last element
procedure: split-at-value a search-value [inclusiveness] ->
list any [symbol:exclusive/inclusive] -> (list:left list:right)
procedure: split-by-pattern pattern a ->
(symbol symbol/ellipsis:... ...) list -> (list:((key . values) ...):matches list:rest)
basic matcher that only supports matching single or repeated elements with multiple ellipses.
creates alist elements for variables in pattern that match elements in list \"a\".
the result is a list with two values: one for the match and one for the unmatched rest.
if pattern did not match, then both values are false. if pattern is null, matches is null and rest is the input list.
unlike other pattern matchers, \"pattern\" is a list and not syntax and so can be passed as a variable.
# example
(split-by-pattern (quote (a b ... c)) (list 1 2 3 4)) -> (((a . 1) (b 2 3) (c . 4)) ())
procedure: tail-or-null a ->
list -> list
return the tail of list or null if there is no tail which is
the case when list is null
procedure: take* count a ->
like srfi-1 take but with reversed argument order (like stream-take from srfi-41) and
returns null if list contains less elements than count instead of raising an exception
procedure: union a ... ->