caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Martin Jambon <martin_jambon@emailuser.net>
To: skaller <skaller@users.sourceforge.net>
Cc: Martin Jambon <martin_jambon@emailuser.net>,
	Luc Maranget <luc.maranget@inria.fr>,
	Christophe Raffalli <christophe.raffalli@univ-savoie.fr>,
	sejourne_kevin <sejourne_kevin@yahoo.fr>,
	caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Request for complete pattern matching
Date: Wed, 23 Nov 2005 14:25:40 -0800 (PST)	[thread overview]
Message-ID: <Pine.LNX.4.63.0511231345300.15641@munge> (raw)
In-Reply-To: <1132781404.25948.78.camel@rosella>

On Thu, 24 Nov 2005, skaller wrote:

> On Wed, 2005-11-23 at 12:56 -0800, Martin Jambon wrote:
>
>> (* toto.ml *)
>> try
>>    while true do
>>      match read_line () with
>>  	/ upper / | / _* "." eos / -> print_endline "looks like a sentence"
>>        | "." | / ("bye"~ space*)+ / -> print_endline "Bye!"; exit 0
>>        | _ -> print_endline "???"
>>    done
>> with End_of_file -> ()
>>
>> Notes:
>> - the stuff between slashes are regexps
>> - "." and the last _ are regular OCaml patterns
>> - regexps are replaced by an identifier which is matched after the
>> arrow using library functions, then it is decided whether to jump to the
>> next case or to execute the user-given expression.
>
> Why can't this be done with 'when' clause?
>
> EG:
>
> 	| ... predicate ... -> ...
>
> replace by
>
> 	| ... variable .. when predicate(variable)

It was not shown in my example, but the predicate can introduce bindings:

     |  String (predicate which also defines x) | Int x -> ...
     | _ -> ...


You can do this:

# let f = function
      `Int n
    | `String / (int as n : int) eos / -> 2 * n
    | _ -> -1;;
val f : _[> `Int of int | `String of string ] -> int = <fun>
# f (`Int 123);;
- : int = 246
# f (`String "2005");;
- : int = 4010
# f (`String "x");;
- : int = -1


> This would seem to fail only in that 'predicate' code
> cannot refer to another predicate, since the variable
> name is not known.
>
> The 'break' implementation, however, may be overtly
> more efficient (save duplicating predicate evaluations).
>
> -- 
> John Skaller <skaller at users dot sf dot net>
> Felix, successor to C++: http://felix.sf.net
>
>

--
Martin Jambon, PhD
http://martin.jambon.free.fr

Store and share your bioinformatics tips at http://wikiomics.org


  reply	other threads:[~2005-11-23 22:25 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-22 22:43 Christophe Raffalli
2005-11-23  5:54 ` [Caml-list] " Luc Maranget
2005-11-23 14:37   ` Christophe Raffalli
2005-11-23 10:06 ` Michal Moskal
2005-11-23 15:26   ` Christophe Raffalli
     [not found] ` <43842069.3070700@yahoo.fr>
2005-11-23 14:47   ` Christophe Raffalli
2005-11-23 18:31     ` Luc Maranget
2005-11-23 20:56       ` Martin Jambon
2005-11-23 21:30         ` skaller
2005-11-23 22:25           ` Martin Jambon [this message]
2005-11-24  9:29         ` Luc Maranget
2005-11-25 23:01           ` Martin Jambon
2005-11-23 20:56       ` Christophe Raffalli
2005-11-24  9:41         ` Luc Maranget

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.63.0511231345300.15641@munge \
    --to=martin_jambon@emailuser.net \
    --cc=caml-list@inria.fr \
    --cc=christophe.raffalli@univ-savoie.fr \
    --cc=luc.maranget@inria.fr \
    --cc=sejourne_kevin@yahoo.fr \
    --cc=skaller@users.sourceforge.net \
    /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;
as well as URLs for NNTP newsgroup(s).