Browse thread
[Caml-list] Pattern matching and strings
[
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: | Sven Luther <luther@l...> |
| Subject: | Re: [Caml-list] Pattern matching and strings |
On Wed, Oct 02, 2002 at 04:12:34PM +0200, Alessandro Baretta wrote: > I have to do a little bit of pattern matching on strings. My > first instict was to write something like the following. > > let foo x = ... > let bar x = ... > ... = function > | "foo" ^ rest -> foo rest > | "bar" ^ rest -> bar rest > | _ -> raise Unrecognized What about : ... = function | str when String.sub str 0 3 = "foo" -> foo (String.sub str 2 (String.length str - 3)) | str when String.sub str 0 3 = "bar" -> bar (String.sub str 2 (String.length str - 3)) | _ -> raise Unrecognized Sure, this code is not very optimal, i guess you could write a nicer function which will test the string incrementally using just String.get or something such, but i suppose it will do the thing you want. Friendly, Sven Luther ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners