CS152 Core ML Sample code

exception Empty
type 'a queue = 'a list
val put : 'a queue * 'a -> 'a queue
val get : 'a queue -> 'a * 'a queue
- unzip [(1, true), (3, false)];
> val it = ([1, 3], [true, false]) : int list * bool list
- flatten [[1], [2, 3, 4], [], [5, 6]];
> val it = [1, 2, 3, 4, 5, 6] : int list
type 'a env = (* you fill in this part *)
exception NotFound of string
val emptyEnv : 'a env = (* ... *)
val bindVar : string * 'a * 'a env -> 'a env = (* ... *)
val lookup  : string * 'a env -> 'a = (* ... *)
fun lookup (name, rho) = rho name
datatype 'a tree = NODE of 'a tree * 'a * 'a tree 
                 | LEAF
datatype order = LESS | EQUAL | GREATER
fun insert cmp =
  let fun ins(x, LEAF) = NODE(LEAF, x, LEAF)
        | ins(x, NODE(left, y, right)) = 
             (case cmp(x, y)
                of LESS    => NODE(ins(x, left), y, right)
                 | GREATER => NODE(left, y, ins(x, right))
                 | EQUAL   => NODE(left, x, right))
  in  ins
  end
datatype 'a set = SET of ('a * 'a -> order) * 'a tree
fun nullset cmp = SET (cmp, LEAF)
datatype rator = PLUS | MINUS | TIMES
datatype 'a exp = VAL of 'a
                | RATOR of rator * 'a exp * 'a exp
type 'a ring = rator -> 'a * 'a -> 'a
val reals : real ring = (fn PLUS => op + | MINUS => op - | TIMES => op * )
infix 3 ++ --
infix 4 **
fun a ++ b = RATOR(PLUS, a, b)
fun a -- b = RATOR(MINUS, a, b)
fun a ** b = RATOR(TIMES, a, b)