1.31. List functions

Start C++ section to tut/examples/tut140a.flx[1 /1 ]
     1: include "std";
     2: open List;
     3: val x : list[int] =
     4:   Cons (1, Cons (2, Cons (3, Empty[int])));
     5: val y = rev x;
     6: iter (proc (x:int) { print x; print ", "; }) x; endl;
     7: iter (proc (x:int) { print x; print ", "; }) y; endl;
     8: 
     9: proc print (x:list[int])
    10: {
    11:   match x with
    12:   | Empty[int] => { print "[]"; }
    13:   | Cons[int] (?h,?t) =>
    14:     {
    15:       print "["; print h;
    16:       iter (proc (x:int) { print ","; print x; }) t;
    17:       print "]";
    18:     }
    19:   endmatch;
    20: }
    21: 
    22: fun add (x:int) (y:int):int = { return x + y; }
    23: 
    24: val x_l_total = fold_left add of (int) 0 x;
    25: val y_l_total = fold_left add of (int) 0 y;
    26: val x_r_total = fold_right add of (int) x 0;
    27: val y_r_total = fold_right add of (int) y 0;
    28: print x_l_total; endl;
    29: print y_l_total; endl;
    30: print x_r_total; endl;
    31: print y_r_total; endl;
    32: 
    33: 
    34: 
End C++ section to tut/examples/tut140a.flx[1]