% /cse/courses/cse341/97wi/sml/sml Standard ML of New Jersey, Version 0.93, February 15, 1993 - tl (hd [1, 2] :: [3, 4, 5]); val it = [3,4,5] : int list - #1(nil, [1, 2]) @ ["A", "B"]; val it = ["A","B"] : string list - map (fn(x) => x + 1) [ "A", "B", "C"]; std_in:4.1-4.37 Error: operator and operand don't agree (tycon mismatch) - map (fn(x) => x + 1) (1, 2, 3); std_in:0.0-0.0 Error: operator and operand don't agree (tycon mismatch) - map rev [[1, 2], [3, 4]]; val it = [[2,1],[4,3]] : int list list - length [1, 1.0, "one"]; std_in:5.8-5.22 Error: operator and operand don't agree (tycon mismatch) - [1,2,3,4] = 1::2::3::4; (* Lists end with nil *) std_in:0.0-0.0 Error: operator and operand don't agree (tycon mismatch) - nil::nil::nil; val it = [[],[]] : 'a list list - val x = 3; - if x mod 2 = 0 then x else x-1; val it = 2 : int - if x mod 2 = 0 then x else if x > 0 then x - 1 else x+1; val it = 2 : int - val L = [(1,2), (3,4), (5, 6), (7,8)]; - rev (tl (rev L)); val it = [(1,2),(3,4),(5,6)] : (int * int) list - map #1 L; val it = [1,3,5,7] : int list - map (fn(x)=> #1 x) L; val it = [1,3,5,7] : int list - val s = "Banana Apple Mango"; - implode (filter (fn (x) => x <> "a") (explode s)); val it = "Bnn Apple Mngo" : string - val L = [[1,2,3],[4,5],[6,7,8],[9],[10,11]]; - map ( map (fn(x:int) => x*x) ) L; val it = [[1,4,9],[16,25],[36,49,64],[81],[100,121]] : int list list - map (fn(y) => (map (fn(x:int) =>x*x) y)) L; val it = [[1,4,9],[16,25],[36,49,64],[81],[100,121]] : int list list - datatype 'a tree = = Leaf of int * 'a | = TwoNode of int * 'a tree * 'a tree | = ThreeNode of int * int * 'a tree * 'a tree * 'a tree; datatype 'a tree con Leaf : int * 'a -> 'a tree con ThreeNode : int * int * 'a tree * 'a tree * 'a tree -> 'a tree con TwoNode : int * 'a tree * 'a tree -> 'a tree - (TwoNode 8 (Leaf (8, Leaf(6, "bob"), Leaf(8, "tom"))); val it = TwoNode (8,Leaf (6,"bob"),Leaf (8,"tom")) : string tree - val L = [1,2,3,4,5,6,7,8,9]; - fun everyOther L = if L = nil then nil = else if tl L = nil then L = else hd L :: everyOther (tl (tl L)); val everyOther = fn : ''a list -> ''a list - everyOther L; val it = [1,3,5,7,9] : int list - fun everyOther nil = nil = | everyOther (x::nil) = x::nil = | everyOther (x  ::y::ys) = x::everyt Other(^H    ys; val everyOther = fn : 'a list -> 'a list - everyOther L; val it = [1,3,5,7,9] : int list - - fun f(i,j) = if i = 0 then j = else if j = 0 then 2*i = else f(i-1,j) + f(i, j-1) + f(i-1,j-1); val f = fn : int * int -> int - f(4,3); val it = 243 : int - f(4,4); val it = 540 : int - f(10,10); val it = 15565770 : int - fun f(0,j) = j = | f(i,0) = 2*i = | f(i,j) = f(i-1,j) + f(i, j-1) + f(i-1,j-1); val f = fn : int * int -> int - f(4,3); val it = 243 : int - f(4,4); val it = 540 : int - f(10,10); val it = 15565770 : int - (* Insertion Sort *) - fun insert x nil = [x] = | insert x (L as y::ys) = if (x:int) <= y then x::L = else y::insert x ys; val insert = fn : int -> int list -> int list - insert 7 [1,3, 6, 8, 9, 10]; val it = [1,3,6,7,8,9,10] : int list - fun insertionSort nil = nil = | insertionSort (x::xs) = insert x (insertionSort xs); val insertionSort = fn : int list -> int list - insertionSort [4, 1, 9, 3, 10, 5, 9, 10, 2]; val it = [1,2,3,4,5,9,9,10,10] : int list