Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Pattern matching and strings
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Chris Hecker <checker@d...>
Subject: [Caml-list] views for strings (was: Pattern matching and strings)

>take to make it work. Probably all it takes is some syntax-magic since the 
>above can be mapped onto the following:

I'm wondering if you couldn't do something with views rather than hacking 
enhanced string matching into caml.

I would much rather have a powerful general feature like Views where you 
could make these string matches somewhat cleaner, than have a hacked-in 
perl-esque regex system that isn't useful if you aren't doing string stuff.

But, having said that, I'm not sure how you'd do your example with 
views.  Even relaxing the "evaluate each view transform once and memoize" 
rule, I can't see how to specify the string prefix to the view transform:

>let foo x = ...
>let bar x = ...
>... = function
>| "foo" ^ rest -> foo rest
>| "bar" ^ rest -> bar rest
>| _ -> raise Unrecognized

Using Okasaki's viewtype syntax, I think you'd want to do something like this:

viewtype string = Prefix of string * string | NoPrefix

match s with
   Prefix("foo",rest) -> foo rest
| Prefix("bar",rest) -> bar rest
| _ -> raise Unrecognized

But the problem is that the parameter to the view transform is just the 
string s, so you can't get the "foo" to it to know whether it's a 
prefix.  You could do

viewtype string * string = Prefix | NoPrefix

But then you'd have to specify the prefix in the match expression, which 
would be useless since there is more than one prefix you're looking 
for.  You could do

viewtype (string list) * string = Prefix of string * string

and pre-specify all the prefixes, but that's totally stupid and useless.  :)

It seems like the view transformation only uses information in one 
direction, from the match expression to a constructor, but it can't get 
information from the available constructors to see if they're possible 
matches.  There's no feedback.

So, maybe views can't make this cleaner, or maybe I just don't know enough 
about them.  Perhaps somebody who's thought about it more could say.


To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: