1: include "std";
2:
3: union float_list =
4: | Empty
5: | Cons of double * float_list
6: ;
7:
8: val t0 = Empty;
9: val nxt = (100.1,t0);
10: val t1 = Cons nxt;
11: val t2 = Cons (200.2,t1);
12:
13: proc print (a: float_list)
14: {
15: match a with
16: | Empty => { print "[]"; }
17: | Cons (?i, ?tail) =>
18: {
19: print i;
20: print " ";
21: print tail;
22: }
23: endmatch;
24: }
25:
26: print t2; endl;
27:
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: include "std";
2:
3: union int_list =
4: | Empty
5: | Cons of int * int_list
6: ;
7:
8: proc print (a: int_list)
9: {
10: match a with
11: | Empty => { print "[]"; }
12: | Cons (?i,?tail) =>
13: {
14: print i;
15: print " ";
16: print tail;
17: }
18: endmatch;
19: }
20:
21: fun mk(i: int, tl:int_list):int_list =
22: {
23: return
24: if(i>0) then mk(i-1,Cons(i,tl))
25: else tl
26: endif
27: ;
28: }
29:
30: val a = mk(10,Empty);
31: print "List= "; print a; endl;
32:
33: fun rev(lst:int_list):int_list =
34: {
35: var result = Empty;
36: proc aux(lst:int_list)
37: {
38: match lst with
39: | Empty => {}
40: | Cons (?head,?tail) =>
41: {
42: result = Cons(head,result);
43: aux(tail);
44: }
45: endmatch;
46: }
47: aux(lst);
48: return result;
49: }
50:
51: print "Reversed= "; print (rev a); endl;
52: