Alist patternmatches a
List tfor some type
tand has one of three forms:
head List.+: tailmatches a list with at least one element. The pattern
headis matched against the first element of the list and
tailis matched against the suffix of the list with the first element removed.
init :+ lastmatches a list with at least one element. The pattern
initis matched against the prefix of the list with the last element removed, and
lastis matched against the last element of the list.
- Aliteral list patternhas the form
[p1, p2, … pn]where
pnare patterns. The patterns
pnare matched against the elements of the list. This pattern only matches if the length of the scrutinee is the same as the number of elements in the pattern. The pattern
matches the empty list.
part1 List.++ part2matches a list which composed of the concatenation of
part2.At least one of
part2must be a pattern with a known list length, otherwise it's unclear where the list is being split. For instance,
[x, y] List.++ restis okay as is
start List.++ [x, y],but just
a ++ bis not allowed.
first : [a] -> Optional a first = cases h +: _ -> Some h  -> None last : [a] -> Optional a last = cases _ :+ l -> Some l  -> None exactlyOne : [a] -> Boolean exactlyOne = cases [_] -> true _ -> false lastTwo : [a] -> Optional (a, a) lastTwo = cases start ++ [a, a2] -> Some (a, a2) _ -> None firstTwo : [a] -> Optional (a, a) firstTwo = cases [a, a2] ++ rest -> Some (a, a2) _ -> None