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.
A pair list is indexable using […] to access a list
element by position—
> pr
Pair(1, 2)
> pr.first
1
> pr.rest
2
> y
2
def: value does not satisfy annotation
value: Pair(1, 2)
annotation: matching(Pair((_ :: String), (_ :: String)))
> y
2
function  | ||
  | ||
binding operator  | ||
Note that the difference between Pair.cons and
PairList.cons is that PairList.cons requires a pair list as its
second argument, which means that it always forms a pair list. In
contrast, Pair.cons allows any value as its second
argument—
> Pair.cons(1, 2)
Pair(1, 2)
PairList[1, 2, 3]
property  | ||
  | ||
  | ||
property  | ||
  | 
> Pair.first(Pair("a", "b"))
"a"
"b"
annotation  | ||||||||
  | ||||||||
annotation  | ||||||||
  | ||||||||
annotation  | ||||||||
  | ||||||||
  | ||||||||
  | ||||||||
  | ||||||||
  | 
The PairList.tuple_of annotation is analogous to the List.tuple_of annotation, but for pair lists.
Static information associated by PairList or PairList.of makes an expression acceptable as a sequence to for in static mode.
function  | ||||||||||||
  | ||||||||||||
  | ||||||||||||
expression  | ||||||||||||
  | ||||||||||||
  | ||||||||||||
repetition  | ||||||||||||
  | ||||||||||||
  | ||||||||||||
  | ||||||||||||
  | ||||||||||||
  | ||||||||||||
  | ||||||||||||
  | 
> 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  | |
function  | ||
  | 
> 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
method  | ||
  | 
> PairList.reverse(PairList[1, 4, 8])
PairList[8, 4, 1]
PairList[8, 4, 1]
method  | ||
  | ||
  | ||
function  | ||
  | 
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[]
method  | ||
  | ||
  | ||
method  | ||
  | 
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
method  | ||
  | ||
  | ||
method  | ||
  | 
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  | |||
#true
#false
#true
#true
method  | |||
1
#false
1
method  | ||
  | ||
  | ||
method  | ||
  | 
2
#false
1
#false
method  | ||
  | 
[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]
method  | ||
  | 
> 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[]
method  | ||
  |