Recursive Types

Below is the code we used in class. In the cslab, it can also be found in /cslab/class/cs243/recurtypes2.ml.


datatype wholeNumber = Zero | OnePlus of wholeNumber;

fun succ(num) = OnePlus(num);

fun asWholeNumber(0) = Zero
   | asWholeNumber(n) = OnePlus(asWholeNumber(n-1));

fun pred(OnePlus(num)) = num;
 

fun plus(Zero , num) = num
   | plus(num, Zero) = num
   | plus(OnePlus(num1), OnePlus(num2)) = 
         plus( num1 , OnePlus(OnePlus(num2)));

fun isLessThanOrEq(Zero, num) = true
   | isLessThanOrEq(num, Zero) = false
   | isLessThanOrEq(OnePlus(num1), OnePlus(num2)) =
              isLessThanOrEq(num1, num2);

fun isLessThan(num, Zero) = false
   | isLessThan(Zero, num) = true
   | isLessThan(OnePlus(num1), OnePlus(num2)) =
      isLessThan(num1, num2);

fun asInt(Zero) = 0
   | asInt(OnePlus(num)) = 1 + asInt(num);

fun times(Zero, num) = Zero
   | times(num, Zero) = Zero
   | times(OnePlus(num1), num3) =
            plus(num3, times(num1, num3));



Thomas VanDrunen
Last modified: Mon Dec 10 10:38:01 CST 2007