Browse thread
Re: Suggestions
- Pierre Weis
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 1995-09-14 (09:50) |
From: | Pierre Weis <weis@p...> |
Subject: | Re: Suggestions |
Continuing the ``where match'' saga ... > the problem is that "when" is not a binder ! > The "where match" is in fact strictly more general than the when: > pat when exp -> > is equivalent to > pat where match exp with true -> > but the when is weaker because it does not bind any variable. You're right. In my mind even your ``where'' construct is not general enough. What you want is the so-called ``continue'' feature of Caml V3.1: a way to ask the pattern matcher to get out from an already selected clause (wherever you can be within its expression part). The continue construct works as this: in a pattern matching like | pat -> expr | pat' -> expr' | ... a ``continue'' statement within expr will exit from the computation of expr, and the pattern matching will go on with the next clause appropriate for pat (presumably pat' -> expr'). Using continue, you can freely mix computation of expr and exits to the rest of the pattern matching, as in: | pat -> begin try let x = ... in if x > 2 then continue else assoc x l with Not_found -> continue end Furthermore ``continue'' subsumes the ``where match'' you proposed: | pat where match expr with pati -> ei is equivalent to | pat -> (match expr with pati -> ei | _ -> continue) (In fact the implemented ``continue'' statement is a bit more complex: pattern matchings with continue are named so that you may exit to another pattern matching from within a more nested one.) In Caml Light, you may simulate a kind of continue statement, using exception handling and functionality. This is a bit less efficient and less readable than a built-in mechanism, but it is acceptable since it is not so common to need ``continue''... ( Here is the encoding trick: Suppose you have a clause pat -> expr that needs a continue statement, inside a given pattern matching: | ... | pat -> expr | pati -> ei Then define a Continue exception and an auxiliary function for the rest of the pattern: exception Continue;; let rest = function | pati -> ei Use ``raise Continue'' when you need a continue statement and rewrite your code as: | ... | pat as p -> begin try expr with Continue -> rest p end | x -> rest x ) Pierre Weis ---------------------------------------------------------------------------- WWW Home Page: http://pauillac.inria.fr/~weis Projet Cristal INRIA, BP 105, F-78153 Le Chesnay Cedex (France) E-mail: Pierre.Weis@inria.fr Telephone: +33 1 39 63 55 98 Fax: +33 1 39 63 53 30 ----------------------------------------------------------------------------