/* Polymorphic linked lists - first version */ using System; using poly; public class cell { private number head; // can't use 'object' - too general! private cell tail; public number CAR { get { return head; } } public cell CDR { get { return tail; } } public cell(number h, cell t) {head=h; tail=t;} public int length() // internal visitor { int ax = 0; // accumulator for (cell i=this;i!=null;i=i.tail) ax++; return ax; } public void print() { Console.Write(head + " "); if (tail==null) Console.Write("\n"); else tail.print(); } } public class polylist { public static int length(cell l) // external visitor { if (l==null) return 0; else return 1+length(l.CDR); } public static void Main() { integer n1 = new integer(3); rational r1 = new rational(1,2); cell l = new cell(n1,new cell(r1,null)); l.print(); for(cell m=l;m!=null;m=m.CDR) m.CAR.inc(); l.print(); } } //complie with csc /r:number.dll polylist.cs