skaller wrote: > On Wed, 2006-11-15 at 10:26 -0800, Erick Tryzelaar wrote: >> Now that we got pattern calculus, can we do something like this? >> >> #import <flx.flxh> >> >> fun foo: int -> int -> int = >> | 5->6 => 6 >> | 7->8 => 8 >> | _ => 9 >> ; >> >> print$ foo 5 6; endl; > > The full pattern stuff only works with typematch, > where the matching is done at compile time. > > In any case 5->6 isn't an expression is it?
You would know better than I :) Caml Light appears to have had this feature though: http://groups.google.com/group/fa.caml/browse_thread/thread/9e3fcfd063afb046/9ceedcb30e215ac5?lnk=st&q=pattern+matching+curried+functions&rnum=7#9ceedcb30e215ac5 Although it used curried notation instead of the arrow notation I used. So, this was apparently supported: fun | 1 1 -> bli | _ 0 -> bla | _ _ -> blu I'm having a lot of fun with that lisp-in-haskell tutorial, and it made me wonder if we could simplify many of the common patterns in that code. For instance, here's my eval function: fun eval (ctx:Context) (expr:Expr):(Context*Expr) => match expr with | LispInt ?n => ctx, LispInt n | LispFn ?f => ctx, LispFn f | LispSymbol ?s => match find (fun (x:string, e:Expr):bool => x == s) ctx with | Some (_, ?x) => ctx, x endmatch | LispList Cons (?x, ?xs) => match eval ctx x with | (?ctx', LispFn ?f) => let ?ctx'', ?es = map_accum_left (the eval) ctx' xs in f ctx es endmatch endmatch ; We almost can do the haskell form right now, we just don't have a way of destructuring the curried arguments. So, I thought that this could be a possibly simpler form: fun eval Context -> Expr -> Context*Expr = | ?ctx -> (LispInt ?n) => ctx, LispInt n | ?ctx -> (LispFn ?f) => ctx, LispFn f | ?ctx -> (LispSymbol ?s) => match find (fun (x:string, e:Expr):bool => x == s) ctx with | Some (_, ?x) => ctx, x endmatch | ?ctx -> (LispList Cons (?x, ?xs)) => match eval ctx x with | (?ctx', LispFn ?f) => let ?ctx'', ?es = map_accum_left (the eval) ctx' xs in f ctx es endmatch ; Oh, and one last thing I've forgotten about. I'd still love if we could do this: match 5 with | 5 | 6 => 7 | _ => 8 endmatch; And for a more complicated example with binding: match (1,2,3) with | (?x, _, ?y) where x == 1 | (?x, ?y, _) => 5 | _ => 6 endmatch -e ------------------------------------------------------------------------- Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys - and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV _______________________________________________ Felix-language mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/felix-language
