1: #line 1405 "./lpsrc/flx_tutorial.pak"
2:
3:
4: union float_list =
5: | Empty
6: | Cons of double * float_list
7: ;
8:
9: val t0 = Empty;
10: val nxt = (100.1,t0);
11: val t1 = Cons nxt;
12: val t2 = Cons (200.2,t1);
13:
14: proc xprint (a: float_list)
15: {
16: match a with
17: | Empty => { print "[]"; }
18: | Cons (?i, ?tail) =>
19: {
20: print i;
21: print " ";
22: xprint tail;
23: }
24: endmatch;
25: }
26:
27: xprint t2; endl;
28:
A more interesting example is now given.
Here, we use a recursive routine to build the list,
and an iterative routine to reverse it.
1: #line 1438 "./lpsrc/flx_tutorial.pak"
2:
3:
4: union int_list =
5: | Empty
6: | Cons of int * int_list
7: ;
8:
9: proc xprint (a: int_list)
10: {
11: match a with
12: | Empty => { print "[]"; }
13: | Cons (?i,?tail) =>
14: {
15: print i;
16: print " ";
17: xprint tail;
18: }
19: endmatch;
20: }
21:
22: fun mk(i: int, tl:int_list):int_list =
23: {
24: return
25: if(i>0) then mk(i-1,Cons(i,tl))
26: else tl
27: endif
28: ;
29: }
30:
31: val a = mk(10,Empty);
32: print "List= "; xprint a; endl;
33:
34: fun rev(lst:int_list):int_list =
35: {
36: var result = Empty;
37: proc aux(lst:int_list)
38: {
39: match lst with
40: | Empty => {}
41: | Cons (?head,?tail) =>
42: {
43: result = Cons(head,result);
44: aux(tail);
45: }
46: endmatch;
47: }
48: aux(lst);
49: return result;
50: }
51:
52: print "Reversed= "; xprint (rev a); endl;
53: