Run like:

flx --test=build/release --static -c tr
FLX_REPORT_COLLECTIONS=1 ./tr

I get no segfaults ... GC working correctly (though needs tuning badly).

/// tr.flx
union tree = | Leaf of int | Node of tree * tree;

fun copy (x:tree) => match x with
  | Leaf ?a => Leaf a
  | Node (?x,?y) => Node (copy x, copy y)
  endmatch
;

fun map (f: int -> int) (x:tree) =>
  match x with 
  | Leaf ?i => Leaf (f i) 
  | Node (?x,?y) => Node (map f x, map f y)
  endmatch
;

fun fold[T] (f: T * int -> T) (init:T) (x:tree) =>
  match x with 
  | Leaf ?i => f (init,i) 
  | Node (?x,?y) => fold f (fold f init x) y
  endmatch
;

fun count (x:tree) => fold (fun (n:int,e:int) => n+1) 0 x;
fun sum (x:tree) => fold (fun (n:int,e:int) => n+e) 0 x;

instance Str[tree] { 
  fun str(x:tree)=> 
    match x with 
    | Leaf ?i => str i 
    | Node (?x,?y) => "["+str x +","+str y+"]"
    endmatch
  ;
}

fun mk12() : tree => Node (Leaf 1, Leaf 2);
fun mk(x: tree, y: tree) => Node (x,y);

proc check(k:int) {
  var tr = mk(mk12(), mk12());
  for var j in 0 upto k do
    tr = mk(tr,tr);
  done

  //println$ str tr;

  var tr2 = copy tr;
  //println$ str tr2;

  var tr3 = map (fun (x:int)=>x * x) tr2;
  //println$ str tr3;
  println$ str k + "->" + str (count tr3) + ", sum=" + str (sum tr3);
}

for var k in 0 upto 200 do
  check k;
done

--
john skaller
skal...@users.sourceforge.net





------------------------------------------------------------------------------
RSA(R) Conference 2012
Save $700 by Nov 18
Register now
http://p.sf.net/sfu/rsa-sfdev2dev1
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to