6.30 Lists
Lists can be constructed using the syntax [expr, ...], which creates list containing the values of the exprs as elements. More precisely, a use of square brackets without a preceding expression implicitly uses the #%brackets form, which is normally bound to construct a list.
A list is indexable using […] to access a list
element by position—
lst.length()
is
List.length(lst)
lst.get(n)
is
List.get(lst, n)
lst.set(n, v)
is
List.set(lst, n, v)
lst.first
is
List.first(lst)
lst.last
is
List.last(lst)
lst.rest
is
List.rest(lst)
lst.insert(n, v)
is
List.insert(lst, n, v)
lst.add(v)
is
List.add(lst, v)
lst.delete(n)
is
List.delete(lst, n)
lst.reverse()
is
List.reverse(lst)
is
List.append(lst, lst2, ...)
lst.take(n)
is
List.take(lst, n)
lst.take_last(n)
is
List.take_last(lst, n)
lst.drop(n)
is
List.drop(lst, n)
lst.drop_last(n)
is
List.drop_last(lst, n)
lst.sublist(n, m)
is
List.sublist(lst, n, m)
is
List.has_element(lst, v, eqls, ...)
lst.find(pred)
is
List.find(lst, pred)
lst.remove(v)
is
List.remove(lst, v)
lst.map(func)
is
List.map(lst, func)
lst.for_each(func)
is
List.for_each(lst, func)
is
lst.copy()
is
List.copy(lst)
lst.to_list()
is
List.to_list(lst)
lst.to_sequence()
is
List.to_sequence(lst)
Static information associated by List or List.of makes an expression acceptable as a sequence to for in static mode.
function | |||||||||
| |||||||||
expression | |||||||||
#%brackets [expr_or_splice, ...] | |||||||||
| |||||||||
repetition | |||||||||
#%brackets [repet_or_splice, ...] | |||||||||
| |||||||||
expression | |||||||||
List[expr_or_splice, ...] | |||||||||
| |||||||||
repetition | |||||||||
List[repet_or_splice, ...] | |||||||||
| |||||||||
| |||||||||
| |||||||||
| |||||||||
| |||||||||
|
The #%brackets form is implicitly used when […] is used in in an expression position. See also Implicit Forms.
> lst
[1, 2, 3]
> lst[0]
1
> lst ++ [4, 5]
[1, 2, 3, 4, 5]
> #%brackets [1, 2, 3]
[1, 2, 3]
binding operator | ||||||
| ||||||
binding operator | ||||||
| ||||||
binding operator | ||||||
#%brackets [bind, ...] | ||||||
| ||||||
binding operator | ||||||
#%brackets [bind, ..., rest] | ||||||
| ||||||
binding operator | ||||||
| ||||||
binding operator | ||||||
| ||||||
| ||||||
| ||||||
|
When & list_bind is used, the rest of the list must match the list_bind. Static information associated by List is propagated to list_bind.
When repet_bind is used and does not impose a predicate or conversion on a matching value (e.g., repet_bind is an identifier), then the corresponding elements of a matching value are not traversed, which means that matching can be constant-time. Using this repetition for the tail a new list similarly avoids traversing the elements.
The #%brackets form is implicitly used when […] is used in in a binding position. See also Implicit Forms.
> y
3
> also_y
3
> xs
[2, 3]
> [x, ...]
[2, 3]
annotation |
|
annotation |
> [1] :: NonemptyList
[1]
> [] :: NonemptyList
::: value does not satisfy annotation
value: []
annotation: NonemptyList
reducer |
> List.insert(["a", "b", "c"], 1, "x")
["a", "x", "b", "c"]
> List.add([2, 3], 1)
[2, 3, 1]
> [2, 3].add(1)
[2, 3, 1]
> block:
[2, 3, 1]
> List.cons(1, [2, 3])
[1, 2, 3]
> [2, 3].insert(0, 1)
[1, 2, 3]
> x
1
> y
[2, 3]
value |
def List.empty :: List = [] |
|
binding operator |
> ["a", "b", "c"].get(1)
"b"
> ["a", "b", "c"][1]
"b"
function |
fun List.first(lst :: NonemptyList) :: Any |
> List.first(["a", "b", "c"])
"a"
> ["a", "b", "c"].first
"a"
> List.last(["a", "b", "c"])
"c"
> ["a", "b", "c"].last
"c"
> List.rest(["a", "b", "c"])
["b", "c"]
> ["a", "b", "c"].rest
["b", "c"]
> List.delete(["a", "b", "c"], 1)
["a", "c"]
> ["a", "b", "c"].set(1, "beta")
["a", "beta", "c"]
["a", "beta", "c"]
repetition |
> block:
[x+1, ...]
[2, 3, 4]
> [List.repet(lst) + 1, ...]
[2, 3, 4]
> List.length([1, 4, 8])
3
> List.length([])
0
> [1, 4, 8].length()
3
> [].length()
0
> List.reverse([1, 4, 8])
[8, 4, 1]
> [1, 4, 8].reverse()
[8, 4, 1]
> List.append([1, 2, 3], [4, 5], [6])
[1, 2, 3, 4, 5, 6]
> [1, 2, 3].append([4, 5], [6])
[1, 2, 3, 4, 5, 6]
function |
|
function |
> [1, 2, 3, 4, 5].take(2)
[1, 2]
> [1, 2, 3, 4, 5].take_last(2)
[4, 5]
> [1].take(2)
List.take: index is out of range
index: 2
valid range: [0, 1]
list: [1]
function |
|
function |
> [1, 2, 3, 4, 5].drop(2)
[3, 4, 5]
> [1, 2, 3, 4, 5].drop_last(2)
[1, 2, 3]
> [1].drop(2)
List.drop: index is out of range
index: 2
valid range: [0, 1]
list: [1]
> [1, 2, 3, 4, 5].sublist(1, 3)
[2, 3]
[2, 3]
function | |||
|
> [1, 2, 3].has_element(2)
#true
> [1, 2, 3].has_element(200)
#false
2
#false
> [1, 2, 3, 2].remove(2)
[1, 3, 2]
function | ||
| ||
function | ||
|
[2, 3, 4]
[2, 3, 4]
1
2
3
> List.sort([1, 3, 2])
[1, 2, 3]
> List.sort([1, 3, 2], math.greater)
[3, 2, 1]
> List.iota(3)
[0, 1, 2]
> List.iota(0)
[]
> [1, 2, 3].copy()
MutableList[1, 2, 3]