% sml Standard ML of New Jersey, Version 110.0.7, September 28, 2000 [CM; autoload enabled] - - (* 1. Basic types = int: 0,1,2,.... ~1, ~2,... = real: 3.14, 2.65, ... = string: "hello", "cs320" .. = bool: true, false = unit: () = *) - - (* ******************************************************************** *) - - (* 2. Arithmetic Operations: +,-,*,/,div, mod, ~*) - 1; val it = 1 : int - 1+2*3; val it = 7 : int - true; val it = true : bool - "hello"; val it = "hello" : string - "cs320"; val it = "cs320" : string - 2.0-1; stdIn:22.1-22.6 Error: operator and operand don't agree [literal] operator domain: real * real operand: real * int in expression: 2.0 - 1 - 4.0/2.0; val it = 2.0 : real - 4 div 2; val it = 2 : int - 5 mod 3; GC #0.0.0.0.1.7: (0 ms) val it = 2 : int - val x=7; val x = 7 : int - val y=8; val y = 8 : int - val z=x*y+3; GC #0.0.0.0.1.9: (0 ms) val z = 59 : int - ~1.0 + 2.0; val it = 1.0 : real - - (* ******************************************************************** *) - - (* 3. string operation *) - (* ^ concatenation *) - val y = "hello"^"there!"; val y = "hellothere!" : string - - (* ******************************************************************** *) - - (* 4. Comparison Operators: =,<,>,<=,>=,<> *) - - val z=2<=10; val z = true : bool - (* Logical Operators: not, andalso, orelse *) - 1<5 andalso 2>10; val it = false : bool - - (* ******************************************************************** *) - - (* 5. Functions *) - fun add4(x) = x+4; val add4 = fn : int -> int - val y = 5*add4(4); val y = 40 : int - - (* type overload *) - fun square(x) = x*x; val square = fn : int -> int - fun square(x:real) = x*x; val square = fn : real -> real - val y = square(2.0); val y = 4.0 : real - fun max3(a:int, b, c) = = if a>b then = if a>c then a = else c = else if b>c then b = else c; val max3 = fn : int * int * int -> int - max3(10, 20, 30); val it = 30 : int - - (* ******************************************************************** *) - - (* 6. Tuples *) - (* List of 2 or more elements of any type, separated by ',', surrounded by () *) - val tup1 = (3, 4.0, "five"); val tup1 = (3,4.0,"five") : int * real * string - val tup2 = (5, 6.0, "four"); val tup2 = (5,6.0,"four") : int * real * string - val x = #2(tup2); val x = 6.0 : real - - (* ML regards each function as taking a single parameter multiple params = single tuple *) - (* All functions return single value, although "multiple values" may be encapsulated into a tuple *) - - fun sumAndDiff(x:int, y) = (x+y, x-y); val sumAndDiff = fn : int * int -> int * int - sumAndDiff(2,3); val it = (5,~1) : int * int - - (* ******************************************************************** *) - - (* 7. List *) - (* Zero or more elements of a single type, separated by ',', surrounded by [] *) - (* [1,2,3], [2.0], ["hi", "hello"], [1,2.0]=>Illegal! *) - (* for any type 'T', there is a type 'T list' *) - - (* Operations on Lists *) - (* empty list: nil, [] = hd([1,2,3]) => 1 = tl([1,2,3]) => [2,3] = hd + tl tak apart lists = ::(cons) + @(concat) construct lists *) - - 2::[2,3,4]; val it = [2,2,3,4] : int list - ["a", "b"]@["c", "d"]; val it = ["a","b","c","d"] : string list - 1::(2::(3::(4::(5::nil)))); GC #0.0.0.0.2.53: (10 ms) val it = [1,2,3,4,5] : int list - - (* ******************************************************************** *) - - (* 8. Recursion *) - (* Factorial *) - - fun fact(n) = = if n=0 then 1 = else n*fact(n-1); val fact = fn : int -> int - fact(0); val it = 1 : int - fact(4); val it = 24 : int - fun listcount(ls) = = if ls = nil then 0 = else 1+listcount(tl(ls)); val listcount = fn : ''a list -> int - listcount([1,2,3,4]); val it = 4 : int - - (* ******************************************************************** *) - - (* 9. Mutual Recursion *) - fun isEven(n) = = if n=0 then true = else isOdd(n-1) = and = isOdd(n) = = if n=0 then false = else isEven(n-1); val isEven = fn : int -> bool val isOdd = fn : int -> bool - isEven(0); val it = true : bool - isOdd(0); val it = false : bool - isEven(3); val it = false : bool - isEven(4); val it = true : bool - - (* ******************************************************************** *) - - (* 10. Pattern Matching *) - (* strength of ML is ability to define functions based on pattern of parameters *) - (* x::xs => matches non-empty list argument, x:hd, xs:tail *) - - fun listCount(nil) = 0 = | listCount(x::xs) = 1+listCount(xs); val listCount = fn : 'a list -> int - listCount([1,2,3,4]); val it = 4 : int - - (* M, N: two integer lists, sorted in increasing order = merge(M, N) -> new list with elements of M, N also sorted *) - - fun merge(M, []) = M = | merge([], N) = N = | merge(x::xs, y::ys) = = if (x:int) int list - merge([1,3,5,8], [2,4,9]); val it = [1,2,3,4,5,8,9] : int list - - (* can also pattern-match function result *) - fun sumAndDiff(x:int, y) = (x+y, x-y); val sumAndDiff = fn : int * int -> int * int - val (sum, diff) = sumAndDiff(10, 5); val sum = 15 : int val diff = 5 : int - - (* ******************************************************************** *) - - (* 11. Let *) - (* need to create temporary values (local variables) within functions *) - (* x^10 x:real *) - fun xten(x:real) = x*x*x*x*x*x*x*x*x*x; val xten = fn : real -> real - xten(2.0); val it = 1024.0 : real - - fun xten(x:real) = = let = val x5 = x*x*x*x*x = in = x5 * x5 = end; val xten = fn : real -> real - xten(2.0); val it = 1024.0 : real - - (* ******************************************************************** *) - - (* 12. Defining new types *) - (* type abbreviation: type foo = int * string * bool; = a. 'type' does not introduce a new type, just an abbreviation = b. datatypes + data constructors build more complex types *) - - datatype fruit = Pear | Apple | Orange; datatype fruit = Apple | Orange | Pear - fun isFruit(f) = f=Pear orelse f=Apple orelse f=Orange; val isFruit = fn : fruit -> bool - - datatype money = Nickel | Dime |Quarter | Bill of int; datatype money = Bill of int | Dime | Nickel | Quarter - fun value(Nickel) = 5 = | value(Dime) = 10 = | value(Quarter) = 25 = | value(Bill(x)) = x*100; val value = fn : money -> int - value(Bill(10)); val it = 1000 : int - - (* ******************************************************************** *) - - (* 13. Module System *) - (* facilitates data encapsulation = allows programmer to group types and functions on those types into a cluster that can be used in limited ways = - prevent unexpected use of code, encourage code reuse *) - - (* Structures *) - structure someStruct = = struct = val a = 5; = fun f(x) = x+1; = datatype color = Red|Blue|Green; = end; structure someStruct : sig datatype color = Blue | Green | Red val a : int val f : int -> int end - someStruct.a; val it = 5 : int - val z= someStruct.f(someStruct.a); val z = 6 : int - structure s = someStruct; structure s : sig datatype color = Blue | Green | Red val a : int val f : int -> int end - s.a; val it = 5 : int - s.Red; val it = Red : someStruct.color - s.f s.a; val it = 6 : int - - (* ******************************************************************** *) - - (* 14. Character type (char) *) - (* old ML: character = string of length one - new ML: character != string of length one *) - - #"x"; val it = #"x" : char - "x"; val it = "x" : string - - (* Functions on char *) - ord(#"a"); (*char -> int*) val it = 97 : int - chr(97); (*int->char*) val it = #"a" : char - str(#"a"); (*char->string*) val it = "a" : string - explode("ab"); (*string->char list*) val it = [#"a",#"b"] : char list - implode([#"a",#"b"]); (*char list -> string*) val it = "ab" : string - -(* ^d *) -%