8.2 Pairs and Pair Lists
A pair combines two values: a “first” value and a “rest” value. A pair list is a listable value that is constructed with pairs and the empty pair list; every non-empty pair list is a pair, a pair is a pair list only if its “rest” is a list.
> pr
Pair(1, 2)
1
2
> y
2
def: value does not satisfy annotation
value: Pair(1, 2)
annotation: matching(Pair((_ :: String), (_ :: String)))
> y
2
Note that the difference between Pair.cons and
PairList.cons is that PairList.cons requires a list as is
second argument, which means that it always forms a list. In
contrast, Pair.cons allows any value as its second
argument—
> Pair.cons(1, 2)
Pair(1, 2)
> Pair.cons(1, [2, 3])
Pair(1, [2, 3])
> Pair.first(Pair("a", "b"))
"a"
"b"
annotation | |
| |
annotation | |
Static information associated by PairList or PairList.of makes an expression acceptable as a sequence to for in static mode.
function | ||||||||||||
| ||||||||||||
expression | ||||||||||||
| ||||||||||||
| ||||||||||||
repetition | ||||||||||||
| ||||||||||||
| ||||||||||||
| ||||||||||||
| ||||||||||||
| ||||||||||||
| ||||||||||||
|
The #%brackets form is implicitly used when […] is used in an expression position. See also Implicit Forms.
> lst
PairList[1, 2, 3]
> lst[0]
1
PairList[1, 2, 3, 4, 5]
> PairList[1, 2, 3]
PairList[1, 2, 3]
binding operator | ||||||||||||
| ||||||||||||
| ||||||||||||
binding operator | ||||||||||||
| ||||||||||||
| ||||||||||||
| ||||||||||||
| ||||||||||||
| ||||||||||||
| ||||||||||||
|
> y
3
> also_y
3
> xs
PairList[2, 3]
> PairList[x, ...]
PairList[2, 3]
Unlike a List, a splice that does not impose a predicate or conversion on a matching value is constant-time only when the splice is at the end of the pattern. Matching N elements to a splice in the middle of a pair list takes at least O(N) time. Along those lines, when multiple splices are among the bind_or_splices, a splice using & pair_list_bind requires O(N) time to produce each N-length candidate match to pair_list_bind.
annotation | |
| |
annotation | |
> PairList[1] :: NonemptyPairList
PairList[1]
::: value does not satisfy annotation
value: PairList[]
annotation: NonemptyPairList
reducer | |
> PairList.cons(1, PairList[2, 3])
PairList[1, 2, 3]
binding operator | |
> x
1
> y
PairList[2, 3]
value | |
| |
binding operator | |
"b"
> PairList["a", "b", "c"][1]
"b"
property | |
|
> PairList.first(PairList["a", "b", "c"])
"a"
"a"
property | |
|
> PairList.last(PairList["a", "b", "c"])
"c"
"c"
property | |
|
> PairList.rest(PairList["a", "b", "c"])
PairList["b", "c"]
PairList["b", "c"]
> PairList.length(PairList[1, 4, 8])
3
> PairList.length(PairList[])
0
3
0
> PairList.reverse(PairList[1, 4, 8])
PairList[8, 4, 1]
PairList[8, 4, 1]
PairList[1, 2, 3, 4, 5, 6]
> PairList.append(PairList[1, 2, 3], PairList[4, 5], PairList[6])
PairList[1, 2, 3, 4, 5, 6]
> PairList.append()
PairList[]
PairList[1, 2]
PairList[4, 5]
PairList.take: list is shorter than the number of elements to take
list length: 1
number to take: 2
PairList[3, 4, 5]
PairList[1, 2, 3]
PairList.drop: list is shorter than the number of elements to drop
list length: 1
number to drop: 2
method | |||
> PairList[1, 2, 3].has_element(2)
#true
> PairList[1, 2, 3].has_element(200)
#false
2
#false
1
#false
[1, 3, 2]
> PairList.map(PairList[1, 2, 3], (_ + 1))
PairList[2, 3, 4]
PairList[2, 3, 4]
1
2
3
method | ||||
| ||||
| ||||
method | ||||
|
PairList[1, 2]
PairList[-1, -2]
> PairList[1, -1, -2, 2].filter(~keep: (_ != -2), ~skip: (_ > 0))
PairList[-1]
PairList[1, 2]
PairList[-1, -2]
> PairList.sort(PairList[1, 3, 2])
PairList[1, 2, 3]
> PairList.sort(PairList[1, 3, 2], (_ > _))
PairList[3, 2, 1]
function | |
> PairList.iota(3)
PairList[0, 1, 2]
> PairList.iota(0)
PairList[]
repetition | |
|
> block:
PairList[2, 3, 4]
> [PairList.repet(lst) + 1, ...]
[2, 3, 4]