From: Jeremy Yallop <jeremy.yallop@ed.ac.uk>
To: Caml List <caml-list@inria.fr>
Subject: ANN: pattern guards
Date: Fri, 29 Jun 2007 15:19:41 +0100 (BST) [thread overview]
Message-ID: <Pine.LNX.4.64.0706291518250.15944@rydell.inf.ed.ac.uk> (raw)
I'm pleased to announce the initial release of `patterns', an OCaml
extension providing general-purposes additions to pattern matching.
This release includes a single feature: "pattern guards"; others will
be made available in the near future.
You can download patterns from
http://code.google.com/p/ocaml-patterns/
Pattern guards generalize "when"-guards to allow arbitrary pattern
matching with binding. After each pattern in a match you can write
one or more binding phrases as follows:
match e with
| patt1 with p1 = e1
...
with pn = en
-> e
| patt2 ...
The expressions e1 ... en are evaluated in turn and matched with
the corresponding patterns p1 ... pn until either a match fails (in
which case matching proceeds with patt2 etc.) or all matches
succeed. Any variables bound in p1 ... pn are in scope within
subsequent guards and within e.
For example, given a function
val lookup : 'a -> ('a * 'b) list -> 'b option
you might write the following
let f env = function
| Var x
with Some v = lookup x env -> ... v ...
instead of the less elegant and less efficient
let f env = function
| Var x
when mem_assoc x env -> ... assoc x env ...
Pattern guards and regular guards can be freely intermixed; for
example, you can write
match e with
| patt when c1
with patt1 = e1
when c2
with patt2 = e2 -> e
| ...
Pattern guards were proposed (for Haskell) in
Martin Erwig and Simon Peyton Jones
Pattern Guards and Transformational Patterns
Haskell Workshop, 2000
See also: http://research.microsoft.com/~simonpj/Haskell/guards.html
Jeremy.
next reply other threads:[~2007-06-29 14:19 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-06-29 14:19 Jeremy Yallop [this message]
2007-06-29 18:26 ` [Caml-list] " skaller
2007-06-29 18:56 ` Jeremy Yallop
2007-06-29 19:31 ` Brian Hurt
2007-06-30 4:09 ` skaller
2007-06-30 4:44 ` Arnaud Spiwack
2007-07-03 9:29 ` Jeremy Yallop
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Pine.LNX.4.64.0706291518250.15944@rydell.inf.ed.ac.uk \
--to=jeremy.yallop@ed.ac.uk \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox