|
Project Information
Featured
Downloads
|
Update: Jake Donham has updated this package to work with OCaml 3.12: http://github.com/jaked/patterns A framework for writing extensions to OCaml pattern matching. Examples included with the distribution:
Conjunctive patterns (as found in F#) generalise "as"-patterns. In standard OCaml the syntax `patt as var' may be used to bind a value simultaneously to both a pattern and a variable; with conjunctive patterns the syntax `patt & patt' may be used to bind a value simultaneously to two patterns. For example, let ((`A a, b) & (c, `B d)) = (`A 3, `B 4) in (a,b,c,d) evaluates to (3, `B 4, `A 3, 4)
Object patterns bind the results of calling an object's methods to other patterns during a pattern match. This makes it more convenient to use objects as structurally-typed records. The notation mirrors that in Jacques Garrigue's pa_oo extension. For example, let {| x = x; y = _ |} =
object method x = 3 method y = 4 method z = 5 end
in
x + 1evaluates to 4
Matching with negative patterns succeeds if the value does not match the pattern given. For example, let nonzero = function
| ~0 -> true
| _ -> false
in (nonzero 4, nonzero 0)evaluates to (true, false)
The infamous n+k patterns (as found in Haskell) offer a "Peano-number" view of integers. Matching an integer value v against `patt+k' (where k is an integer literal) succeeds, binding patt to v-k, if v>=k. For example let pred = function
| x+1 -> x
| 0 -> 0
in (pred 10, pred 0)evaluates to (9, 0) Release 0.3 included
which are gone for now, but should reappear in a future release. Links: |