common

File common.str
Author unknown
Since unknown

This module contains basic functionality for manipulating lists.




Statistics


General
Lines of code 89
Stratego
Module number 1 (100% documented)
Constructor number 0
Overlay number 0
Strategy number 8 (100% documented)
Rule number 0
DynamicRule number 0



Strategy summary


at-end(Strategy s) Apply s to the Nil of a list common.str
at-init(Strategy s1, Strategy s2) Applies s1 to all elements in a list, except the last, where s2 is applied common.str
at-last(Strategy s) Apply s to the last Cons ([_]) of a list common.str
at-suffix(Strategy s) Apply a strategy to some suffix of a list common.str
at-suffix-rev(Strategy s) Apply a strategy to some suffix of a list common.str
at-tail(Strategy s) Apply a strategy to the tail of a list common.str
back-split-at(ATerm n) Splits a list in two, with the second part containing the last n elements and and first part containing all elements except the last n common.str
common-prefix Returns the common prefix of two lists common.str
conc Concatenates all lists of a tuple common.str
concat Concatenates a list of lists into one list common.str
copy n/a common.str
copy(Strategy s) Makes n copies of a term into a list of duplicates, applying the strategy s to every copy common.str
drop(ATerm n) Drops the first n elements from a list common.str
drop(Strategy isn) Drops a number of terms from the front of a list common.str
drop-until(Strategy s) Drops elements from the start of a list until s succeeds common.str
drop-while(Strategy s) Drops elements from the start of a list while s succeeds common.str
elem Succeeds if the term is in the list common.str
elem(Strategy eq) Succeeds if the term is in the list, using the given strategy for determining equality common.str
eliminate-common-suffix Eliminates all elements at the end of the two lists that are equal common.str
fetch(Strategy s) Find first list element for which s succeeds common.str
fetch-elem(Strategy s) Return first list element for which s succeeds common.str
first-last Returns a list with the first and the last element of the input list common.str
flatten-list Completely flattens a list and its sublists to a single list common.str
foldl(Strategy s) Left folds a list common.str
foldr(Strategy s1, Strategy s2) Right folds a list common.str
foldr(Strategy s1, Strategy s2, Strategy f) Right folds a list common.str
for-each-pair(Strategy s) n/a common.str
init n/a common.str
is-list Succeeds if the input term is a list common.str
last Returns the last element of a list common.str
length Returns the length of a list common.str
list(Strategy s) n/a common.str
list-combinations Returns a list of combinations by choosing one element from every list, in every possible combination common.str
list-fold(Strategy s, ATerm acc) n/a common.str
list-loop(Strategy s) Iterative loop over a list applying s to each element common.str
list-some(Strategy s) Apply a strategy to some elements in a list common.str
list-tokenize(ATerm sep) Breaks a list into multiple parts (tokens) common.str
list-tokenize(Strategy sep) Breaks a list into multiple parts (tokens) common.str
listbu(Strategy s) Applies a strategy to a list in bottom up order common.str
listbu1(Strategy s) n/a common.str
listdu(Strategy s) n/a common.str
listdu2(Strategy s1, Strategy s2) n/a common.str
listtd(Strategy s) n/a common.str
ltrim(Strategy s) n/a common.str
makeConc Concats two elements if both elements are lists common.str
map(Strategy s) Apply a strategy to each element of a list common.str
mapconcat(Strategy s) Transform the elements of a list into lists (map) and concatenate into a single list (concat) common.str
matrix-transpose Transposes an n by m matrix common.str
member Succeeds if the first input term is a member of the second common.str
number(Strategy s) n/a common.str
reverse Reverses a list common.str
reverse(Strategy s) Reverses a list and applies s to all the elements common.str
reverse-map(Strategy s) In reverse order, apply a strategy to each element of a list common.str
rtrim(Strategy s) Trim elements from the end of a list common.str
separate-by Separates the elements of the list by the specified separator common.str
separate-by(Strategy sep) Separates the elements of the list by the specified separator common.str
separate-by(ATerm sep) Separates the elements of the list by the specified separator common.str
split-at(Strategy s) n/a common.str
split-at(ATerm n) Splits the list in two sublists, containing elements from 0 to n and from n onwards common.str
split-fetch(Strategy s) Splits a list in two parts at the first point where s succeeds common.str
split-fetch-keep(Strategy s) Splits a list in two parts at the point where s succeeds, keeping the element at which s succeeded common.str
split-init-last Splits a list into a tuple of its init list and last element common.str
split-last n/a common.str
splitmap(Strategy s, ATerm n) Splits a list after n elements and applies strategy s to the first sublist common.str
take(Strategy isn) Take the first n elements of a list, given by isn common.str
take(ATerm n) Returns the first n elements of a list, fails if list has fewer than n elements common.str
take-until(Strategy s) Take elements from the start of a list until s succeeds common.str
take-while(Strategy s) Take elements from the start of a list while s succeeds common.str
takemap(Strategy s, ATerm n) Returns the n first elements after s has been applied to them common.str
trim(Strategy s) Trim elements from both start and end of a list common.str
upto n/a common.str

Rule summary


MkCons Makes a Cons out of a tuple with head and tail common.str
split-Cons Splits a Cons into a tuple with head and tail common.str
thread-map(Strategy s) Applies s to each element in the list, keeping along a separate context term common.str



Strategy details


ATerm at-end(Strategy s)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s is applied to Nil ([]) and must return a list: List(a) -> List(a)

Apply s to the Nil of a list.


type List(a) -> List(a)

 
ATerm at-init(Strategy s1, Strategy s2)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s1 s1 a -> b
Strategy s2 s2 a -> b

Applies s1 to all elements in a list, except the last, wheres2 is applied.


type List(a) -> List(b)

 
ATerm at-last(Strategy s)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s Is applied to the [x] and must return a list: List(a) -> List(a)

Apply s to the last Cons ([_]) of a list.


type List(a) -> List(a)

 
ATerm at-suffix(Strategy s)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s is applied to the suffix : List(a) -> b

Apply a strategy to some suffix of a list.

The longest suffix (that is, the first application in a list)is preferred.


type List(a) -> List(a or b)

 
ATerm at-suffix-rev(Strategy s)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s is applied to the suffix : List(a) -> b

Apply a strategy to some suffix of a list.

The shortest suffix (that is, the last application in a list)is preferred.


type List(a) -> List(a or b)

 
ATerm at-tail(Strategy s)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s is applied to the tail: List(a) -> List(b)

Apply a strategy to the tail of a list.


type List(a) -> List(a or b)

 
ATerm back-split-at(ATerm n)
File common.str
Author unknown
Since unknown
 
Parameters
ATerm n n - the number of elements to split at (counting from the back)

Splits a list in two, with the second part containing the lastn elements and and first part containing all elements except thelast n.


type List(a) -> (List(a), List(a))

 
ATerm common-prefix
File common.str
Author unknown
Since unknown
 

Returns the common prefix of two lists.

Examples:<common-prefix>([1,2,3], [1,2,4,5]) => [1,2]<common-prefix>([1,2,3], [2,3,4]) => []


type (List(a), List(a)) -> List(a)

 
ATerm conc
File common.str
Author unknown
Since unknown
 

Concatenates all lists of a tuple.


type (List(a), List(a), ...) -> List(a)

 
ATerm concat
File common.str
Author unknown
Since unknown
 

Concatenates a list of lists into one list.

Example: <concat> [[1,2],[3,4],[5,6]] => [1,2,3,4,5,6]


type List(List(a)) -> List(a)

 
ATerm copy(Strategy s)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s s a -> b

Makes n copies of a term into a list of duplicates, applyingthe strategy s to every copy. The first input term is the integer n, the second is the term to duplicate.

Example: <copy(\ "foo" -> "bar" \)> (3, "foo") => ["bar","bar,"bar"]


type Int * a -> List(n)

 
ATerm drop(ATerm n)
File common.str
Author unknown
Since unknown
 
Parameters
ATerm n n - the number of elements to drop

Drops the first n elements from a list. If the list hasfewer than n elements, the strategy fails.


type List(a) -> List(a)

 
ATerm drop(Strategy isn)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy isn _ -> Int

Drops a number of terms from the front of a list.

The number is specified by the strategy argument, which shouldproduce an integer.


type List(a) -> List(a)

 
ATerm drop-until(Strategy s)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s s a -> _

Drops elements from the start of a list until s succeeds. Thefirst element at which s succeeds and all following it will bereturned.

Example: <drop-until(?3)> [2,2,3,4] => [3,4]


type List(a) -> List(a)

 
ATerm drop-while(Strategy s)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s s a -> _

Drops elements from the start of a list while s succeeds. Thefirst element at which s fails and all following it will bereturned.

Example: <drop-while(?2)> [2,2,3,4] => [3,4]


type List(a) -> List(a)

 
ATerm elem
File common.str
Author unknown
Since unknown
 

Succeeds if the term is in the list.


type (a, List(a)) ->? List(a)

 
ATerm elem(Strategy eq)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy eq (a, a) ->? b

Succeeds if the term is in the list, using the givenstrategy for determining equality.


type (a, List(a)) ->? List(a or b)

 
ATerm eliminate-common-suffix
File common.str
Author unknown
Since unknown
 

Eliminates all elements at the end of the two lists that are equal.Only works correctly on lists of equal length!

Example: <eliminate-common-suffix>([1,3,4], [1,2,4]) => ([1,3], [1,2])


type (List(a), List(a)) -> (List(a), List(a))

 
ATerm fetch(Strategy s)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s a -> b

Find first list element for which s succeeds.


type List(a) -> List(a or b)

 
ATerm fetch-elem(Strategy s)
File common.str
Author unknown
Since unknown
 

Return first list element for which s succeeds.


type List(a) -> a

 
ATerm first-last
File common.str
Author unknown
Since unknown
 

Returns a list with the first and the last element ofthe input list. For the empty list and the singletonlist, this is equivalent to id.


type List(a) -> List(a)

 
ATerm flatten-list
File common.str
Author unknown
Since unknown
 

Completely flattens a list and its sublists to a single list.

See list-misc-test for examples.


type List(rec x(a or List(x))) -> List(a)

 
ATerm foldl(Strategy s)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s (a, b) -> b

Left folds a list. That is, the strategy sis applied as a binary operator between all adjacent elements in the list.foldr starts by applying s to b and the first element inthe list. b is therefore the starting point of the folding.

Example: <foldl(add)> ([1,2,3,4], 0)


type (List(a), b) -> b

 
ATerm foldr(Strategy s1, Strategy s2)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s1 [] -> b
Strategy s2 (a, b) -> b

Right folds a list. That is, the strategy s2 is applied as a binary operator between all adjacent elements in the list.foldr starts by applying s2 to the last element in the listand the result of s1. s1 is therefore the starting point ofthe folding.

Example: <foldr(!0, add)> [1,2,3,4] => 10


type List(a) -> b

 
ATerm foldr(Strategy s1, Strategy s2, Strategy f)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s1 [] -> b
Strategy s2 (c, b) -> b
Strategy f a -> c

Right folds a list. That is, the strategy s2 is applied as a binary operator between all adjacent elements in the list.foldr starts by applying s2 to the last element in the listand the result of s1. s1 is therefore the starting point ofthe folding.

The additional parameter strategy f is applied to each element justbefore each folding step.

Example: <foldr(!0, add, inc)> [1,2,3,4] => 14


type List(a) -> b

 
ATerm is-list
File common.str
Author unknown
Since unknown
 

Succeeds if the input term is a list.


type List(a) -> _

 
ATerm last
File common.str
Author unknown
Since unknown
 

Returns the last element of a list.

Fails if applied to the empty list.


type List(a) -> a

 
ATerm length
File common.str
Author unknown
Since unknown
 

Returns the length of a list.


type List(a) -> Int

 
ATerm list-combinations
File common.str
Author unknown
Since unknown
 

Returns a list of combinations by choosing one element from every list, in every possible combination.

Examples:

$ <list-combinations> [[1, 2]][[1],[2]]

$ <list-combinations> [[1, 2], []][]

$ <list-combinations> [[1, 2], ["a", "b"]][[1,"a"],[2,"a"],[1,"b"],[2,"b"]]

$ <list-combinations> [][[]]


type List(List(a)) -> List(List(a))

 
ATerm list-loop(Strategy s)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s Strategy to apply to every element (a -> _)

Iterative loop over a list applying s to each element.


type List(a) -> List(a)

 
ATerm list-some(Strategy s)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s s a -> b

Apply a strategy to some elements in a list. The elementsof the original list will be kept unchanged when the strategy s fails.


type List(a) -> List(b)

 
ATerm list-tokenize(ATerm sep)
File common.str
Author unknown
Since unknown
 
Parameters
ATerm sep List of separator elements.

Breaks a list into multiple parts (tokens).

The term argument sep is a list of delimiters (elements that separatetokens from one another), which is used to split the listinto multiple tokens. The result is a list of lists, i.e. a listof tokens.


type List(a) -> List(List(a))

 
ATerm list-tokenize(Strategy sep)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy sep s a -> fail?

Breaks a list into multiple parts (tokens).

The strategy argument sep is used to split the list. Wherever itsucceeds, the original list is split, and the parts are returnedas a list of lists, i.e. a list of tokens.


type List(a) -> List(List(a))

 
ATerm listbu(Strategy s)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s s List(a) -> List(b)

Applies a strategy to a list in bottom up order. That is to say,the strategy s will be applied to successively longer excerptsof the list, starting from the end.

At the first invocation, s will be applied to the tail of the list,and is expected to return a new list. The last element of the listwill then be added in front of this result, and s is applied tothis. The recursion continues through all elements from last tofirst, each time on a longer list, hence "bottom up".

Example: <listbu(![9 | <id>])> [1,2,3,4] => [9,1,9,2,9,3,9,4,9]


type List(a) -> List(b)

 
ATerm listbu1(Strategy s)
File common.str
Author unknown
Since unknown
 

No information available.



 
ATerm makeConc
File common.str
Author unknown
Since unknown
 

Concats two elements if both elements are lists. Otherwise, constructsa Conc term.

Generic term construction is used to avoid infinite recursion: makeConcis used in the compilation of Conc itself.


type List(a) * List(b) -> List(a|b)
type a * b -> Conc(a,b)

 
ATerm map(Strategy s)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s s is applied to all elements: a -> b

Apply a strategy to each element of a list.


type List(a) -> List(b)

 
ATerm mapconcat(Strategy s)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s a -> List(b)

Transform the elements of a list into lists (map)and concatenate into a single list (concat).

Note: equivalent to map(s); concat


type List(a) -> List(b)

 
ATerm matrix-transpose
File common.str
Author unknown
Since unknown
 

Transposes an n by m matrix. The matrix must be represented asa list of n elements, where each element is a list of length m.The element of the inner lists may be of any type.

Example: <matrix-transpose> [[1,2],[3,4]] => [[1,3],[2,4]]


type List(List(a)) -> List(List(a))

 
ATerm member
File common.str
Author unknown
Since unknown
 

Succeeds if the first input term is a member of the second.


type a * List(a) -> a

 
ATerm reverse
File common.str
Author unknown
Since unknown
 

Reverses a list.


type List(a) -> List(a)

 
ATerm reverse(Strategy s)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s a -> b

Reverses a list and applies s to all the elements.


type List(a) -> List(b)

 
ATerm reverse-map(Strategy s)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s s is applied to all elements: a -> b

In reverse order, apply a strategy to each element of a list.


type List(a) -> List(b)

 
ATerm rtrim(Strategy s)
File common.str
Author unknown
Since 0.9.5
 
Parameters
Strategy s should succeed for all elements that have to be trimmed.

Trim elements from the end of a list

Removes the longest sublist from the end of a list, for whichall elements satisfy the strategy s.


type List(a) -> List(a)

 
ATerm separate-by
File common.str
Author unknown
Since unknown
 

Separates the elements of the list by the specified separator.The separate-by variant that uses a term argument is prefered.


type (sep, List(a)) -> List(a or sep)

 
ATerm separate-by(Strategy sep)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy sep Strategy that results in a separator.

Separates the elements of the list by the specified separator.The separate-by variant that uses a term argument is prefered.


type List(a) -> List(a or sep)

 
ATerm separate-by(ATerm sep)
File common.str
Author unknown
Since unknown
 
Parameters
ATerm sep Separator term

Separates the elements of the list by the specified separator.


type List(a) -> List(a or sep)

 
ATerm split-at(Strategy s)
File common.str
Author unknown
Since unknown
 

No information available.


note Alias for split-fetch/1

 
ATerm split-at(ATerm n)
File common.str
Author unknown
Since unknown
 

Splits the list in two sublists, containing elements from 0 ton and from n onwards.

Example: <split-at(|4)>[1,2,3,4,5,6,7,8,9] => ([1,2,3,4], [5,6,7,8,9])


type List(a) -> (List(a), List(a))

 
ATerm split-fetch(Strategy s)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s a ->? _

Splits a list in two parts at the first point where s succeeds.

The element to which s was applied is not part of the result. split-fetchfails if s cannot be applied to any of the elements.

Examples:<split-fetch(\ 3 -> 6 \)> [1, 2, 3] => ([1,2], [])<split-fetch(\ 3 -> 6 \)> [2, 3, 4] => ([2], [4])<split-fetch(\ 3 -> 6 \)> [3, 4, 5] => ([], [4,5])not(<split-fetch(\ 3 -> 6 \)> [8, 7, 6])


type List(a) -> (List(a), List(a))

 
ATerm split-fetch-keep(Strategy s)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s a -> b

Splits a list in two parts at the point where s succeeds, keeping theelement at which s succeeded.

Unlike split-fetch, this strategy keeps the dividing element as part ofthe result as the second element in the triple: (before, split, after)

split-fetch-keep fails if s cannot be applied to any of the elements.


type List(a) -> (List(a), b, List(a))

 
ATerm split-init-last
File common.str
Author unknown
Since 0.9.4
 

Splits a list into a tuple of its init list and last element.

Example:<split-init-last> [1, 2, 3, 4] => ([1, 2, 3], 4)<split-init-last> [1] => ([], 1)


type List(a) -> (List(a), a)

 
ATerm split-last
File common.str
Author unknown
Since unknown
 

No information available.


note Alias for split-init-last.

 
ATerm splitmap(Strategy s, ATerm n)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s s a -> b
ATerm n n - the number of elements to apply s to, from the start

Splits a list after n elements and applies strategy s to thefirst sublist. The second sublist is left untouched. Disregarding side-effects, splitmap is equal to !(<take(|n); map(s)>, <drop(|n)>). If side-effects areconsidered, note that application of s happens whiletraversing and splitting.


type List(a) -> (List(b), List(a))

 
ATerm take(Strategy isn)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy isn isn _ -> Int

Take the first n elements of a list, given by isn. Thestrategy argument isn must produce an integer, whichgives the length of the sublist to return. If there arenot enough elements, this strategy fails.


type List(a) -> List(a)

 
ATerm take(ATerm n)
File common.str
Author unknown
Since unknown
 
Parameters
ATerm n n The number of elements to take.

Returns the first n elements of a list, failsif list has fewer than n elements.


type List(a) -> List(a)

 
ATerm take-until(Strategy s)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s s a -> _

Take elements from the start of a list until s succeeds. Each element of the list is tested against s, starting atthe head of the list. For as long as s does not succeed, the elements are accumulated in a list, which is returned atthe instant s fails. The actual term returned by s isignored. If s never succeeds, the entire list is returned.

Example: <take-until(?2; !3)> [3,3,2,4] => [3,3]


type List(a) -> List(a)

 
ATerm take-while(Strategy s)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s s a -> _

Take elements from the start of a list while s succeeds.Each element of the list is tested against s, starting atthe head of the list. For as long as s succeeds, the elementsare accumulated in a list, which is returned as s fails, orthe end of the list is reached. The actual term returned bys is ignored.

Example: <take-while(?2 ; !3)> [2,2,3] => [2,2]


type List(a) -> List(a)

 
ATerm takemap(Strategy s, ATerm n)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s n - The number of elements to retrieve
ATerm n s a -> b

Returns the n first elements after s has been applied to them.With the exception of side effects, takemap(s|n) is equal totake(|n); map(s). The difference when considering side-effectsis that s is applied while taking elements, so if s has aside-effect these will be performed, even if take fails.


type List(a) -> List(b)

 
ATerm trim(Strategy s)
File common.str
Author unknown
Since 0.9.5
 
Parameters
Strategy s s a -> -

Trim elements from both start and end of a list.

Removest the longest sublist from both start and end of alist for which all elements satisfy s.


type List(a) -> List(a)

 

Rule details


ATerm MkCons
File common.str
Author unknown
Since unknown
 

Makes a Cons out of a tuple with head and tail. Thetail must be a list, but may be the empty list.


type (a, List(a)) -> List(a)

 
ATerm split-Cons
File common.str
Author unknown
Since unknown
 

Splits a Cons into a tuple with head and tail.


type List(a) -> (a, List(a))

 
ATerm thread-map(Strategy s)
File common.str
Author unknown
Since unknown
 
Parameters
Strategy s s a * b -> a' * b'

Applies s to each element in the list, keeping along a separatecontext term.

For each element in the list, a tuple (a, b) is constructed andgiven to s. From the result, (a', b'), a' goes to the final listreturned by this strategy, and b' becomes the new b as s isapplied to the next element.

Example: <thread-map(add ; !(<id>, <id>))> ([1,2,3,4], 1) => ([2,4,7,11],11)


type List(a) * b -> List(a') * b'