Oh YES! Here we go (takes AGES to compile but it works):

//////////////////
include "std/datatype/ralist";
open Ralist;

fun _match_ctor_Cons[T] (x:ralist[T]) =>not ( ralist_empty x);
fun _match_ctor_Empty[T] (x:ralist[T]) => ralist_empty x;

fun _ctor_arg_Cons[T] (x:ralist[T]) : T * ralist[T] =
{
  var elt : T;
  var tail : ralist[T];
  ralist_uncons (x, &elt, &tail);
  return elt,tail;
}

var x : ralist[int] = RAnil[int];
x = ralist_cons (1, x);
x = ralist_cons (2, x);
x = ralist_cons (4, x);
x = ralist_cons (9, x);
x = ralist_cons (16, x);
println$ "List=" + x.str;

match x with
| Cons (?elt, Cons (?elt2, ?tail)) =>
  println$ "Head1="+ elt.str + "Second=",+elt2.str + ", Tail="+ tail.str;
| Cons (?elt, ?tail) =>
  println$ "Head="+ elt.str + ", Tail="+ tail.str;
| Empty => 
  println$ "Empty list";
endmatch;
/////////////////
~/felix>build/release/host/bin/flx --test=build/release ta
List=16,9,4,2,1
Head1=16, Second=9, Tail=4,2,1
/////////////////

Note, we even overloaded the standard list constructor names so 
the patten match code is textually identical to the code for a standard list.
And that nested pattern matches work too!

Also note the match checkers and extractor are defined
with Felix code (not with bindings to C++). Polymorphism even
seems to work :)

You may be curious why I'm so ecstatic about this.

That's because you haven't grokked exactly how powerful this mechanism is.
Consider for example ..

        * Modelling regexps as pattern matches

or try this: 

        match s with
        | Split (?left, ?right) => ...

the point of which is:

        | Split (?left, Split (?middle, ?right)) => 

Writing that procedurally:

        var left, tmp = split s;
        var middle, right = split tmp;

which sucks by comparison (note .. Ieft out the check if the split worked?)

--
john skaller
skal...@users.sourceforge.net
http://felix-lang.org




------------------------------------------------------------------------------
Want excitement?
Manually upgrade your production database.
When you want reliability, choose Perforce.
Perforce version control. Predictably reliable.
http://pubads.g.doubleclick.net/gampad/clk?id=157508191&iu=/4140/ostg.clktrk
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to