Any time you define a name, you can use a pattern instead. For example:
[fred, petra] = [12, 13]
|
defines fred to have the value 12 and petra to have the value 13.
A pattern describes the structure you are expecting for the value. When the value is computed it is matched against the pattern and, if the match is successful, the names in the pattern are bound to those parts of the value. Our example is exactly equivalent to:
temp = [12, 13];
fred = temp?0, is_list temp && is_list_len 2 temp = error "pattern match failed"; petra = temp?1, is_list temp && is_list_len 2 temp = error "pattern match failed"; |
where temp is an invisible, anonymous symbol.
You can pattern match on any of nip2’s data structures and types. You can use:
You can nest patterns in any way you like. Patterns are useful in conjunction with list comprehensions, see §6.6.5.
You can’t use patterns in function arguments in the current version, hopefully this will added shortly.