Browse thread
The Implicit Accumulator: a design pattern using optional arguments
[
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: | -- (:) |
| From: | Jonathan Bryant <jtbryant@v...> |
| Subject: | Re: [Caml-list] The Implicit Accumulator: a design pattern using optional arguments |
On Jun 27, 2007, at 2:31 PM, Brian Hurt wrote: > Actually, what I'd like is a more powerful regular expression engine- This is off the top of my head, but I've always thought it would be nice to have built-in regexps using a Scanf style. Instead of using $1, $2 (or \1, \2, depending on your language), have each parenthesized argument be passed to a function, and the whole thing is a function of a string parameter. If a scanf conversion is used, that is the type passed to the function, otherwise it's simply passed as a string. Something like: # let re = /(foo|bar|baz)/ (fun x -> match x with | "foo" -> ... etc ... );; re : string -> 'a = <fun> # let simple_phone_number = /(%d)-(%d)-(%d)/ (fun x y z -> (x,y,z) );; simple_phone_number : string -> (int * int * int) = <fun> I don't know how hard this would be to implement. It would require some scanf style compiler magic, but it overcomes scanf's shortcoming that it doesn't allow character classes. It might even be possible for use in pattern matching: # let f str = match str with | /foo/ -> (* It's a foo *) | /bar/ -> (* It's a bar *) | /baz/ -> (* It's a baz *) | /(%d)-(%d)-(%d)/ -> (* It's a phone number *) | _ -> (* Not matched *) ;; f : string -> 'a = <fun> Any idea how hard this would be? --Jonathan