-
Notifications
You must be signed in to change notification settings - Fork 22
Logic Based Patterns
- Syntax
- and &rest subpattterns
or &rest subpattterns
They match when all/some of the subpatterns matches against the element. For example,
(match x
((or (list 1 a)
(cons a 3))
a))
matches against both (1 2)
and (4 . 3)
and returns 2 and
4, respectively. Also,
(match x
((and (list 1 _)
(list _ 2))
t))
is same as below.
(match x
((list 1 2)
t))
- Syntax
- not subpattern
It does not match when subpattern matches. The variables used in subpattern is not visible in the body.
- Syntax
- guard subpattern1 test-form {generator-form subpattern2}*
- test-form
- a predicate form, evaluated.
- generator-form
- a form that produce a value, which are then matched against the next subpattern2.
The object is first matched against subpattern1. If that fails, the whole match fails. Otherwise, test-form is evaluated. When the result is true, then each of generator-form is evaluated and matched against corresponding subpattern2.
If subpattern1 is of the form (or <pat1> <pat2> ...)
however, it tries to match with one of the or-subpatterns such that test-form is satisfied.
Example:
(match (list 2 5)
((guard (list x y) ; subpattern1
(= 10 (* x y))) ; test-form
t))
;; --> t
(match '(1)
((guard (or (list x) x) ; subpattern1
(evenp x)) ; test-form
x))
;; --> error, since '(1) gets matched with x, after failing evenp on matching (list x)
For a quickstart, go to the Basics section. Below is for the people already familiar with Optima.