(* Collected ML code of Section 11.4 from Kenneth C. Louden, Programming Languages Principles and Practice 2nd Edition Copyright (C) Brooks-Cole/ITP, 2003 *) (* page 495 *) fun fact n = if n = 0 then 1 else n * fact(n - 1); fun fact (n: int): int = if n = 0 then 1 else n * fact (n - 1); (* page 498 *) fun append ([] , L) = L | append (h :: t , L) = h :: append (t , L); fun append (x,y) = case x of [] => y | (h::t) => h :: append (t,y); fun fact 0 = 1 | fact n = n * fact (n - 1); fun hd (h::_) = h; fun hd (h::_) = h | hd [] = raise Empty; fun square x: real = x * x; (* page 500 *) fun constList [] = true | constList [a] = true | constList (a::b::L) = a = b andalso constList (b::L); open TextIO; (* dereference the TextIO structure *) output(stdOut,inputLine(stdIn)); (* pages 501-502 *) fun printstuff () = ( output(stdOut,"Hello\n"); output(stdOut,"World!\n") ); printstuff (); fun gcd (u,v) = if v = 0 then u else gcd (v, u mod v) ; fun euclid () = ( output(stdOut, "Enter two integers:\n"); flushOut(stdOut); let val u = Int.fromString(inputLine(stdIn)); val v = Int.fromString(inputLine(stdIn)) in case (u,v) of (SOME x, SOME y) => ( output(stdOut,"The gcd of "); output(stdOut,Int.toString(x)); output(stdOut," and "); output(stdOut,Int.toString(y)); output(stdOut," is "); output(stdOut,Int.toString(gcd(x,y))); output(stdOut,"\n") ) | _ => output(stdOut, "Bad input.\n") end ) type Salary = real; type Coords = real * real; datatype Direction = North | East | South | West; (* page 503 *) fun heading North = 0.0 | heading East = 90.0 | heading South = 180.0 | heading West = 270.0 ; datatype 'a BST = Nil | Node of 'a * 'a BST * 'a BST; val tree = Node("horse", Node( "cow", Nil, Node("dog",Nil,Nil) ), Node("zebra",Node("yak",Nil,Nil), Nil)); fun leftchild (Node(data,left,right)) = left | leftchild Nil = raise Empty; fun print_tree Nil = () | print_tree (Node (data,left,right)) = ( print_tree left; output(stdOut,data); output(stdOut,"\n"); print_tree right); print_tree tree; (* page 504 *) fn x => x * x; (fn x => x * x) 4; val square = fn x => x * x; val rec fact = fn n => if n = 0 then 1 else n * fact (n - 1); fun make_double f = fn x => f (x,x); val square = make_double (op * ); val double = make_double (op +); square 3; double 3; val double_square = double o square; double_square 3; (* page 505 *) val x = ref 0.0; (* create a variable *) x := 1.0; (* assign it a new value *) !x; fun make_new_balance (newbalance: real) = let val balance = ref newbalance in fn amount => if !balance < amount then raise Fail "Insufficient funds!" else ( balance := !balance - amount; ! balance ) end; val withdraw1 = make_new_balance 100.00; val withdraw2 = make_new_balance 100.00; withdraw1 20.00; withdraw2 50.00; withdraw1 20.00; (* withdraw2 60.00; *) (* pages 506-507 *) fun map f [] = [] | map f (h::t) = (f h):: map f t; val square_list = map square; square_list [1,2,3]; map (fn x => x + x) [1,2,3]; fun gcd (u,v) = if v = 0 then u else gcd (v , u mod v); fun gcd u v = if v = 0 then u else gcd v (u mod v); fun plus x y = x + y; plus 2 3; val plus2 = plus 2; plus2 3; fun append L1 L2 = L1 @ L2; val append_to_one = append [1]; append_to_one [2,3];