2017-08-27

helpers for working with lists

part of sph-lib

map-slice, map-segments, iterate-three, produce, split-by-pattern, fold-multiple, list-index-value, list-sort-with-accessor, ...

(sph list)

- any->list
- any->list-s
- compact
- complement
- complement-both
- complement-p
- consecutive
- contains-all?
- contains-some?
- contains?
- containsq?
- containsv-some?
- containsv?
- convolve
- count-value
- count-value-with-limit
- count-with-limit
- define-list
- delete-duplicates-sorted
- difference
- difference+intersection
- difference+intersection-p
- difference-p
- drop*
- each-first-middle-last
- each-in-index-range
- each-slice
- each-with-index
- every-fold
- every-map
- every-or-index
- false-if-null
- filter-append-map
- filter-not
- filter-produce
- first-if-single
- first-intersection
- first-intersection-p
- first-or-false
- first-or-null
- flat?
- flatten
- fold-integers
- fold-multiple
- fold-multiple-right
- fold-multiple-with-continue
- fold-segments
- fold-slice
- fold-span
- fold-unless
- fold-unless-check-init
- fold-until
- group-consecutive
- group-split-at-matches
- improper-list-split-at-last
- insert-second
- integer->list
- interleave
- intersection
- intersection-p
- iterate-three
- iterate-three-with-stop+end
- length-greater-one?
- length-one?
- list-bind
- list-deselect
- list-distribute
- list-distribute-sorted
- list-index-value
- list-indices
- list-let
- list-logical-condition?
- list-logical-contains?
- list-logical-match
- list-page
- list-prefix?
- list-q
- list-qq
- list-replace-last
- list-replace-last-n
- list-select
- list-set-add
- list-set-equal?
- list-set-eqv?
- list-set-subset?
- list-sort-by-list
- list-sort-with-accessor
- list-suffix?
- list-tail-ref
- map-apply
- map-c
- map-consecutive
- map-first
- map-fold
- map-integers
- map-map
- map-one
- map-segments
- map-selected
- map-slice
- map-span
- map-unless
- map-with-index
- pair->list
- pair-bind
- pair-fold-multiple
- pair-map
- pair-reverse
- pattern-match-min-length
- produce
- produce-controlled
- produce-unless
- replace
- replace-at-once
- replace-value
- simplify
- simplify-list
- sph-list-description
- splice
- splice-last-list
- split-at-last
- split-at-value
- split-by-pattern
- tail-or-null
- take*
- true->list
- true->list-s
- union

procedure## signature

## description

a ->

any -> list

wraps a non-list argument in a list

syntax## signature

a

procedure## signature

## description

a ->

list -> list

keep only true elements in list. removes all boolean false values

procedure## signature

## description

lists ... ->

list ... -> list

delete elements from the first list that are included in the other lists

procedure## signature

## description

a b ->

list list -> (list list)

delete elements in both lists that are included in both lists

procedure## signature

= list1 rest ... ->

procedure## signature

## description

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## signature

## description

a values ->

list ... -> boolean

test if argument "a" contains all of the given values

procedure## signature

## description

a values ->

list ... -> boolean

test if argument "a" contains any of the given values

procedure## signature

## description

a value [member] ->

list any [procedure:{any list -> boolean/any}] -> boolean

return a boolean indicating if list "a" contains "value"

procedure## signature

a value ->

procedure## signature

## description

a values ->

list ... -> boolean

test if argument "a" contains any of the given values

procedure## signature

a value ->

procedure## signature

## description

a b ->

list list -> list

returns the discrete, linear convolution of two one-dimensional sequences.

the length of the result will be (a-length + b-length - 1).

example use case: modelling the effect of a linear time-invariant system on a signal

procedure## signature

## description

value a [equal?] ->

any list -> integer

count occurences of "value" in list

procedure## signature

## description

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## signature

## description

pred limit a ... ->

procedure integer list ... -> integer

like "count" but with an optional parameter for a count at which to stop counting

syntax## signature

name a ...

procedure## signature

## description

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## signature

## description

lists ... ->

list ... -> list

result in a list of elements not included in all given lists

procedure## signature

## description

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## signature

## description

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## signature

## description

equal-f lists ... ->

{any any -> boolean} list ... -> list

like

procedure## signature

## description

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## signature

## description

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## signature

## description

f start end a ... ->

procedure integer integer list ... ->

untested.

call f only for elements in index range between "start" and "end" inclusively

procedure## signature

## description

f slice-length a ->

procedure:{list ->} integer list ->

apply f to each slice of slice-length elements from list.

procedure## signature

## description

f a ... ->

procedure:{index element ... ->} list ... ->

apply f to each element and its index in list a

procedure## signature

## description

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## signature

## description

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## signature

## description

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" failed

procedure## signature

a ->

procedure## signature

## description

f lists ... ->

apply filter-map and then apply append on the result

procedure## signature

## description

f a ->

{any -> boolean} list -> list

like filter but keeps only values where the result of f is false.

same as (filter (compose not f) list)

procedure## signature

## description

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## signature

## description

a ->

list -> any/list

give the first element of a list if the list has only one element, otherwise give the list

procedure## signature

## description

a b ->

list list -> any

give the first found element that is included in both lists

procedure## signature

## description

equal-f a b ->

{any any -> boolean} list list -> any

like first-intersection but the procedure for comparing elements can be specified

procedure## signature

## description

a ->

list -> any/false

give the first element of a list if it is not null, otherwise false

procedure## signature

## description

a ->

results in the first element of a list if it is not null, otherwise null

procedure## signature

## description

a ->

list -> boolean

true if the list does not contain a list

procedure## signature

## description

a ->

list -> (non-list ...)

replace sublists with their content, resulting in a list that does not contain lists

procedure## signature

## description

count init f ->

integer {integer memo -> any} -> memo

fold over integers from 0 to count - 1

procedure## signature

## description

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## signature

## description

f a r ... ->

procedure list any ... -> any

like fold-multiple but works through the list elements from last to first

procedure## signature

## description

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## signature

## description

f size init a ->

{any:state element ... -> any:state} integer any:state list -> any

fold over each overlapping segment with length "size".

example:

(fold-segments f 2 #t (list 4 5 6 7))

is equivalent to

(f 4 5) (f 5 6) (f 6 7)

procedure## signature

## description

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## signature

## description

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## signature

## description

f stop? default init a ... ->

{any ... -> any} {any -> boolean/any} any any list ... -> any

like fold, but returns "default" if "stop?" is true

procedure## signature

f stop? default init a ... ->

procedure## signature

## description

f init stop? a ->

procedure any procedure:{any -> boolean} list -> any

end folding if "stop?" is true

procedure## signature

## description

filter-f a ->

{any -> boolean} list -> list

wrap multiple elements that consecutively match "filter-f" in a list

procedure## signature

## description

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## signature

a ->

pair:improper-list -> (list any:non-pair)

(1 2 . 3) -> ((1 2) 3)

procedure## signature

## description

e a ->

any list -> list

insert "e" as the second element into list "a"

procedure## signature

## description

a ->

any -> any/list

wrap the argument in a list, but only if it is an integer. otherwise give the argument

procedure## signature

## description

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## signature

## description

lists ... ->

list ... -> list

result in a list of all elements which are contained in all given lists

procedure## signature

## description

equal-f rest ... ->

procedure:{any any -> boolean} list ... -> list

like "intersection" but the predicate for comparing the list elements can be specified

procedure## signature

## description

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, a list of unmodified previous list elements, a list of the following list elements

and an arbitrary count of custom values that are updated to the result of the call to "f"

procedure## signature

## description

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

procedure## signature

## description

a ->

list -> boolean

true if list length is greater than one. possibly faster than (> (length a) 1).

has-multiple-elements?

procedure## signature

## description

a ->

list -> boolean

test if list length equals one. possibly faster than (= (length a) 1).

syntax## signature

a lambda-formals body ...

procedure## signature

## description

a indices ->

list (integer ...) -> list

return a new, possibly smaller, list consisting of values not at specified indices

procedure## signature

## description

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## signature

## description

a indices default ->

like list-distribute but faster. works only correctly for indices lists that are sorted ascending

procedure## signature

## description

a value [equal-f] ->

get the index of value in list

procedure## signature

## description

f a ->

procedure:{any -> boolean} list -> (integer ...)

create a list of all indices for which f results in true

syntax## signature

a lambda-formals body ...

procedure## signature

## description

a ->

any -> boolean

true if "a" is a list-logical condition

procedure## signature

## description

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## signature

## description

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## signature

## description

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## signature

## description

a prefix ... ->

list any ... -> boolean

true if the given "prefix" elements exist in order at the beginning of list.

examples:

(list-prefix? (list 3 2 4) 3 1) -> #f

(list-prefix? (list 3 2 4) 3 2) -> #t

syntax## signature

a ...

syntax## signature

a ...

procedure## signature

## description

a replacement ->

list any/procedure:{any -> any} -> list

replace the last element in a list.

if replacement is a procedure, it is called with the last element and if the procedure result is a list then the result is appended

procedure## signature

## description

n a replacement ->

list integer any/procedure:{any ... -> any/list} -> list

if replacement is a procedure, it is called with the last "n" elements and if the procedure result is a list then the result is appended

procedure## signature

## description

a indices ->

list (integer ...) -> list

return a new list consisting of values at indices

procedure## signature

## description

= 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## signature

## description

a ... ->

list ... -> boolean

true if all elements of the given lists appear in all others.

uses "equal?" for element equality comparison

procedure## signature

## description

a ... ->

list ... -> boolean

like "list-set-equal?" but uses "eqv?" for element equality comparison

procedure## signature

= rest ... ->

procedure## signature

## description

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## signature

## description

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## signature

## description

a suffix ... ->

list any ... -> boolean

true if the given "suffix" elements exist in order at the end of list.

see also "list-prefix?"

procedure## signature

a b ->

procedure## signature

## description

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## signature

## description

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## signature

## description

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## signature

## description

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## signature

## description

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 the each new value of states.

example: (map-fold (l (a index) (list (+ a index) (+ 1 index))) (list 1 2 3) 0)

procedure## signature

## description

count f ->

integer {integer -> any} -> list

map over integers from 0 to count - 1

procedure## signature

## description

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## signature

## description

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## signature

## description

len f a ->

procedure:{any ... -> any} integer list -> list

map over each overlapping segment of length len

procedure## signature

## description

select? f a ... ->

procedure procedure list ... -> list

apply f only to elements for which "select?" is true. non-matched 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## signature

## description

slice-length f a ->

integer procedure:{any ... -> any} list -> list

call "f" with each "slice-length" number of consecutive elements of "a"

procedure## signature

## description

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"

procedure## signature

## description

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## signature

f a ... ->

procedure:{integer:index any:element ... -> any} list ... -> list

procedure## signature

a ->

pair -> list

syntax## signature

a (b c) body ...

procedure## signature

## description

f a init ... ->

{pair any -> any} list any ... -> any

like fold-multiple but applying f to the pairs of list

procedure## signature

## description

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## signature

## description

a ->

pair -> pair

reverse the order of values in a pair.

example: (pair-reverse (pair 1 2)) -> (2 . 1)

procedure## signature

## description

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## signature

## description

f a ... ->

procedure:{any ... -> any} list ... -> list

apply "f" to each ordered combination of elements from all lists, the cartesian product,

and return the results in a list.

for example (produce f (1 2) (4 5) (6)) is equivalent to ((f 1 4 6) (f 1 5 6) (f 2 4 6) (f 2 5 6))

procedure## signature

## description

f mappers lists ... ->

{any ... -> any} (procedure:{procedure:{any -> any} list -> list} ...) any/list ... -> list

experimental.

apply "f" to each ordered combination of elements from one or multiple lists, the cartesian product,

and return the results in a list.

the combinations passed to "f" are obtained by nested application of the procedures in the second argument list.

there should be as many lists as mappers.

accepts multiple lists, multiple mappers and non-list arguments.

example:

(produce-controlled f (f1 f2 f3) (1 2) (4 5) (6 7))

is equivalent to

(f1 (lambda (a) (f2 (lambda (b) (f3 (lambda (c) (f a b c)) (6 7))) (4 5))) (1 2))

procedure## signature

## description

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## signature

a select? replacement ->

list procedure any -> list

procedure## signature

## description

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## signature

a search-value replacement [equal-f] ->

list any any [procedure:{any any -> boolean}] -> list

procedure## signature

a ->

any/list -> list/pair/any

list with one element -> element

list with two non-pair elements -> pair

procedure## signature

## description

a ->

list -> list

examples:

(((1 2))) -> (1 2)

(((1 2) (3))) -> ((1 2) (3))

removes extra nesting

variable

procedure## signature

## description

predicate a ->

{list -> boolean} list -> list

splice elements that are lists and match predicate

procedure## signature

## description

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## signature

a ->

list -> (list list)

procedure## signature

a search-value [inclusiveness] ->

list any [symbol:exclusive/inclusive] -> (list:left list:right)

procedure## signature

## description

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## signature

a ->

list -> list

procedure## signature

## description

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## signature

## description

a ->

any -> list/false

wraps a true non-list argument in a list

syntax## signature

a

procedure## signature

a ... ->