sml galaxy-gfisher[1]: Standard ML of New Jersey, Version 0.93, February 15, 1993 val it = () : unit - - (* CHAPTERS 1 AND 5: SIMPLE EXPRESSIOS AND FUNCTIONS *) - - 2+2; val it = 4 : int - fun TwoPlusTwo() = 2+2; val TwoPlusTwo = fn : unit -> int - TwoPlusTwo(); val it = 4 : int - TwoPlusTwo; val it = fn : unit -> int - fun XPlusTwo(x) = x+2; val XPlusTwo = fn : int -> int - XPlusTwo(2); val it = 4 : int - fun XPlusY(x,y) = x+y; std_in:8.20 Error: overloaded variable cannot be resolved: + - fun XPlusY(x,y: int) = x+y; val XPlusY = fn : int * int -> int - XPlusY(2,2); val it = 4 : int - fun XPlusY(x: int, y) = x+y; val XPlusY = fn : int * int -> int - XPlusY(2,2); val it = 4 : int - val XPlusY = fn(x,y: int) => x+y; val XPlusY = fn : int * int -> int - XPlusY(2,2); val it = 4 : int - val s = "xyz"; val s = "xyz" : string - val r = 2.5; val r = 2.5 : real - "Hello " ^ "world."; val it = "Hello world." : string - "xyz" < "abc"; val it = false : bool - (1<2) or (2<1); std_in:23.7-23.8 Error: unbound variable or constructor: or std_in:23.1-23.14 Error: operator is not a function operator: bool in expression: (< : overloaded (1,2)) bogus - (1<2) orelse (2<1); val it = true : bool - (1<2) andalso (2<1); val it = false : bool - if (1<2) then "yes" else "no"; val it = "yes" : string - if (1<2) then "yes"; = ; std_in:32.20 Error: syntax error found at SEMICOLON - - (* CHAPTER 2: TYPE CONSISTENCY *) - - 2.5/2; std_in:0.0-0.0 Error: operator and operand don't agree (tycon mismatch) operator domain: real * real operand: real * int in expression: / (2.5,2) - truncate(2.5)/2; std_in:0.0-0.0 Error: operator and operand don't agree (tycon mismatch) operator domain: real * real operand: int * int in expression: / (truncate (2.5),2) - 2 div 2; val it = 1 : int - 2.5 / real(2); val it = 1.25 : real - if 1=1 then 1 else 2.5; std_in:35.1-35.22 Error: rules don't agree (tycon mismatch) expected: bool -> int found: bool -> real rule: false => 2.5 - if 1 then 2 else 3; std_in:0.0-0.0 Error: case object and rules don't agree (tycon mismatch) rule domain: bool object: int in expression: (case 1 of true => 2 | false => 3) - - (* CHAPTER 3: VARIABLES AND ENVIRONMENTS *) - - val var1 = 10; val var1 = 10 : int - var2 = 20; std_in:0.0-0.0 Error: unbound variable or constructor: var2; - val Var1 = 10; val Var1 = 10 : int - val Var' = 10; val Var' = 10 : int - val x = 10; val x = 10 : int - fun f() = x; val f = fn : unit -> int - f(); val it = 10 : int - val x = 20; val x = 20 : int - f(); val it = 10 : int - - (* CHAPTER 4: TUPLES AND LISTS *) - - val t = (1, 2.5, "three"); val t = (1,2.5,"three") : int * real * string - val tn = (1, 2.5, ("three", true)); val tn = (1,2.5,("three",true)) : int * real * (string * bool) - val t1 = (1); val t1 = 1 : int - #1(t); val it = 1 : int - #2(t); val it = 2.5 : real - #4(t); std_in:62.1-62.5 Error: operator and operand don't agree (record labels) operator domain: {4:'Y, '...Z} operand: int * real * string in expression: (fn {4=4,...} => 4) (t) - val l = [1,2,3,4]; val l = [1,2,3,4] : int list - hd(l); val it = 1 : int - tl(l); val it = [2,3,4] : int list - 0::l; val it = [0,1,2,3,4] : int list - l; val it = [1,2,3,4] : int list - l@[5,6]; val it = [1,2,3,4,5,6] : int list - l; val it = [1,2,3,4] : int list - 1::nil; val it = [1] : int list - 1::2::3::nil; val it = [1,2,3] : int list - ["1",2,3]; std_in:73.1-73.9 Error: operator and operand don't agree (tycon mismatch) operator domain: string * string list operand: string * int list in expression: "1" :: 2 :: 3 :: nil - - explode("abc"); val it = ["a","b","c"] : string list - implode(explode("abc")); val it = "abc" : string - - val nl = [1,2,3,[4,5],6]; std_in:77.10-77.24 Error: operator and operand don't agree (tycon mismatch) operator domain: int list * int list list operand: int list * int list in expression: (4 :: 5 :: nil) :: 6 :: nil - - (* CHAPTER 5: MORE ON FUNCTIONS *) - - fun reverse(L) = = if L = nil then nil = else reverse(tl(L)) @ [hd(L)]; val reverse = fn : ''a list -> ''a list - reverse(l); val it = [4,3,2,1] : int list - - (* CHAPTER 6: PATTERNS IN FUNCTION DEFINITIONS *) - - fun reverse(nil) = nil = | reverse(x::xs) = reverse(xs) @ [x]; val reverse = fn : 'a list -> 'a list - reverse(l); val it = [4,3,2,1] : int list (* SEE FIGURE 6.1 ON PAGE 51 *) - - fun sumLists(nil) = 0 = | sumLists(nil::YS) = sumLists(YS) = | sumLists((x::xs)::YS) = x + sumLists(xs::YS); val sumLists = fn : int list list -> int - fun sumLists2(ll) = = if ll = nil then 0 = else if hd(ll) = nil then sumLists2(tl(ll)) = else hd(hd(ll)) + sumLists2(tl(hd(ll)) :: tl(ll)); val sumLists2 = fn : int list list -> int - val ll = [[1,2], nil, [3], [4,5,6]]; val ll = [[1,2],[],[3],[4,5,6]] : int list list - sumLists(ll); val it = 21 : int - sumLists2(ll); val it = 21 : int - fun sumLists(nil) = 0 = | sumLists(nil::YS) = sumLists(YS) = | sumLists(x::xs::YS) = x + sumLists(xs::YS); std_in:56.27-56.46 Error: operator and operand don't agree (tycon mismatch) operator domain: 'Z list * 'Z list operand: 'Z list * int in expression: + : overloaded (x,sumLists ( )) std_in:56.29 Error: overloaded variable not defined at type symbol: + type: 'Z list - - (* CHAPTER 7: LET *) - - fun hundredthPower(x:real) = = let = val four = x*x*x*x; = val twenty = four*four*four*four*four; = in = twenty*twenty*twenty*twenty*twenty = end; val hundredthPower = fn : real -> real - hundredthPower(1.00001); val it = 1.00100049516174 : real - fun hundredthPower(x:real) = = let = val x = x*x*x*x; = val x = x*x*x*x*x = in = x*x*x*x*x = end; val hundredthPower = fn : real -> real - hundredthPower(1.00001); val it = 1.00100049516174 : real - - fun split(nil) = (nil,nil) = | split([a]) = ([a],nil) = | split(a::b::cs) = = let = val (M,N) = split(cs) = in = (a::M, b::N) = end; val split = fn : 'a list -> 'a list * 'a list - split([1,2,3,4,5]); val it = ([1,3,5],[2,4]) : int list * int list - fun merge(nil,M) = M = | merge(L,nil) = L = | merge(L as x::xs, M as y::ys) = = if (x:int) int list - merge([1,2,3],[4,5]); val it = [1,2,3,4,5] : int list - fun mergeSort(nil) = nil = | mergeSort([a]) = [a] = | mergeSort(L) = = let = val (M,N) = split(L); = val M = mergeSort(M); = val N = mergeSort(N); = in = merge(M,N) = end; val mergeSort = fn : int list -> int list - mergeSort([5,4,3,2,1]); val it = [1,2,3,4,5] : int list - - (* CHAPTER 9: INPUT/OUTPUT *) - - fun printList(nil) = () = | printList(x::xs) = = (print(x:int); print("\n"); printList(xs)); val printList = fn : int list -> unit - printList([1,2,3]); 1 2 3 val it = () : unit - fun readList(file) = = if end_of_stream(file) then nil = else input(file,1) :: readList(file); val readList = fn : instream -> string list - readList(open_in("test.dat")); val it = ["1","2","\n","a","b","\n"] : string list - - (* CHAPTER 12: TYPES AND DATATYPES *) - - type vector = real list; type vector = real list - val v = [1.0, 2.0] : vector; val v = [1.0,2.0] : vector - val w = [1.0, 2.0]; val w = [1.0,2.0] : real list - v=w; val it = true : bool - - datatype fruit = Apple | Pear | Grape; datatype fruit con Apple : fruit con Grape : fruit con Pear : fruit - fun isApple(x) = (x=Apple); val isApple = fn : fruit -> bool - - datatype intOrReal = = IntVal of int | = RealVal of real; datatype intOrReal con IntVal : int -> intOrReal con RealVal : real -> intOrReal - - datatype intOrRealOrStringOrListOfSame = = I of int | = R of real | = S of string | = L of intOrRealOrStringOrListOfSame list; datatype intOrRealOrStringOrListOfSame con I : int -> intOrRealOrStringOrListOfSame con L : intOrRealOrStringOrListOfSame list -> intOrRealOrStringOrListOfSame con R : real -> intOrRealOrStringOrListOfSame con S : string -> intOrRealOrStringOrListOfSame - val mixedList = L[I(10), R(2.5), S("xyz"), L[I(20), R(5.0)]]; val mixedList = L [I 10,R 2.5,S "xyz",L [I #,R #]] : intOrRealOrStringOrListOfSame -