initial
[prop.git] / tools / demos / list2.pcc
blobf9f8269ccfa22303c5023a24a5b6da875c1efb43
1 #include <iostream.h>
3 datatype List<T> = nil | cons(class T, List<T>);
5 template <class T>
6    int length(List<T> l)
7    {  match (l) {
8          case nil:       return 0;
9          case cons(_,l): return 1 + length(l);
10       }
11    }
13 template <class T>
14    ostream& operator << (ostream& out, List<T> l)
15    {  match (l) {
16          case nil:       return out;
17          case cons(h,t): out << h; 
18                          if (t != nil) out << ", "; 
19                          return out << t;  
20       }
21    }
23 instantiate datatype List<int>;
25 int main()
26 {  List<int> l = cons(1,cons(2,cons(3,nil)));
27    cout << "length [" << l << "] = " << length(l) << '\n';
28    return 0;