Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
Copyright 2002 Elsa L. Gunter SML Compiler is on the AFS system at /usr/local/sml/bin/sml A (possibly better, non-PowerPoint) text version of this lecture can be found at For the SML code for today’s lecture see
Copyright 2002 Elsa L. Gunter WWW Addresses for SML ftp://ftp.research.bell- labs.com/dist/smlnj/release/110/110-smlnj.exeftp://ftp.research.bell- labs.com/dist/smlnj/release/110/110-smlnj.exe labs.com/cm/cs/what/smlnj/doc/basis/pages/sml-std- basis.htmlhttp://cm.bell- labs.com/cm/cs/what/smlnj/doc/basis/pages/sml-std- basis.html
Copyright 2002 Elsa L. Gunter Books on SML Supplemental texts (not required) –Elements of ML Programming, by Jeffrey D. Ullman, on Prentice Hall –ML for the Working Programmer, by Lawrence C. Paulson, on Cambridge University Press
Copyright 2002 Elsa L. Gunter Session in SML Standard ML of New Jersey, Version , September 28, 2000 [CM&CMB] - (* Read-eval-print loop; expressions and declarations *) ; val it = 5 : int - val test = 3 < 2; val test = false : bool
Copyright 2002 Elsa L. Gunter SML Expressions and Declarations - (* = At top-level, an expression = ; = is treated as an abbreviation for the declaration = val it = ; =*) - "Hi there"; val it = "Hi there" : string
Copyright 2002 Elsa L. Gunter SML Top-level Expressions and Declarations - (* ^ is string concatenation *) - it ^ " my good friend"; val it = "Hi there my good friend" : string - (* () pronounced "unit" is the result for functions that don’t have a result *) - print "Hello world\n"; Hello world val it = () : unit
Copyright 2002 Elsa L. Gunter Overloading for Basic Arithmetic - val x = 5 + 7; val x = 12 : int - val y = x * 2; val y = 24 : int - val z = ; val z = 1.58 : real
Copyright 2002 Elsa L. Gunter Overloading but No Coercion - val w = y + z; stdIn: Error: operator and operand don't agree [tycon mismatch] operator domain: int * int operand: int * real in expression: y + z
Copyright 2002 Elsa L. Gunter Using SML Code From a File File named test.sml contains 3 + 2; val x = 5 + 7; val y = x * 2; val z = ; val w = y + z;
Copyright 2002 Elsa L. Gunter - use "A:\\SML_Lect\\test.sml"; [opening A:\SML_Lect\test.sml] val it = 5 : int val x = 12 : int val y = 24 : int val z = 1.58 : real Compiler Output
Copyright 2002 Elsa L. Gunter A:\SML_Lect\test.sml: Error: operator and operand don't agree [tycon mismatch] operator domain: int * int operand: int * real in expression: y + z Compiler Output
Copyright 2002 Elsa L. Gunter Booleans (aka Truth Values) - true; val it = true : bool - false; val it = false : bool - if y > x then 25 else 0; val it = 25 : int
Copyright 2002 Elsa L. Gunter Booleans - 3 > 1 andalso 4 > 6; val it = false : bool - 3 > 1 orelse 4 > 6; val it = true : bool - not (4 > 6); val it = true : bool
Copyright 2002 Elsa L. Gunter Functions - fun plus_two n = n + 2; val plus_two = fn : int -> int - plus_two 17; val it = 19 : int - val plus_two = fn n => n + 2; val plus_two = fn : int -> int - plus_two 14; val it = 16 : int
Copyright 2002 Elsa L. Gunter Values fixed at declaration time - val x = 12; val x = 12 : int - fun plus_x y = y + x; val plus_x = fn : int -> int - plus_x 3; val it = 15 : int
Copyright 2002 Elsa L. Gunter Values fixed at declaration time - val x = 7; val x = 7 : int - plus_x 3; val it = 15 : int
Copyright 2002 Elsa L. Gunter Functions with more than one argument - fun add_three (x:int) y z = x + y + z; val add_three = fn : int -> int -> int -> int - val t = add_three 6 3 2; val t = 11 : int
Copyright 2002 Elsa L. Gunter Partial application of functions - val h = add_three 5 4; val h = fn : int -> int - h 3; val it = 12 : int - h 7; val it = 16 : int
Copyright 2002 Elsa L. Gunter Functions as arguments - fun thrice f x = f (f (f x)); val thrice = fn : ('a -> 'a) -> 'a -> 'a - thrice plus_two; val it = fn : int -> int - it 4; val it = 10 : int - thrice (fn s => "Hi! " ^ s) "Good-bye!"; val it = "Hi! Hi! Hi! Good-bye!" : string
Copyright 2002 Elsa L. Gunter Recursive Functions - fun factorial 0 = 1 = | factorial n = n * factorial (n - 1); val factorial = fn : int -> int - factorial 5; val it = 120 : int - - (* fun is needed for recursion function declarations *)
Copyright 2002 Elsa L. Gunter Tuples - val s = (5,"hi",3.2); val s = (5,"hi",3.2) : int * string * real - val (a,b,c) = s; val a = 5 : int val b = "hi" : string val c = 3.2 : real
Copyright 2002 Elsa L. Gunter Tuples - val d = ((1,4,62),("bye",15),73.95); val d = ((1,4,62),("bye",15),73.95) : (int * int * int) * (string * int) * real - val (p,(st,_),_) = d; val p = (1,4,6) : int * int * int val st = "bye" : string
Copyright 2002 Elsa L. Gunter Tuples - fun fst_of_3 (x,_,_) = x; val fst_of_3 = fn : 'a * 'b * 'c -> 'a - s; val it = (5,"hi",3.2) : int * string * real - fst_of_3 s; val it = 5 : int - fst_of_3 d; val it = (1,4,62) : int * int * int
Copyright 2002 Elsa L. Gunter Records - val teacher = {Name = "Elsa L. Gunter", ss = (119,73,6244), age = 102}; val teacher = {Name="Elsa L. Gunter",age=102,ss=(119,73,6244)} : {Name:string, age:int, ss:int * int * int}
Copyright 2002 Elsa L. Gunter Records - val {ss = (s1,s2,s3), Name = elsa, age = years} = teacher; val elsa = "Elsa L. Gunter" : string val years = 102 : int val s1 = 119 : int val s2 = 73 : int val s3 = 6244 : int
Copyright 2002 Elsa L. Gunter Records and Tuples - val q = (280,{student = {Name = "Joseph Martins", = ss = (325,40,1276), = age = 19}, = instructor = teacher});
Copyright 2002 Elsa L. Gunter Records and Tuples val q = (280, {instructor={Name="Elsa L. Gunter",age=102,ss=(119,73,6244)}, student={Name="Joseph Martins",age=19,ss=(325,40,1276)}}) : int * {instructor:{Name:string, age:int, ss:int * int * int}, student:{Name:string, age:int, ss:int * int * int}}
Copyright 2002 Elsa L. Gunter Tuples are Records - val strange = (1,"f",2) = {3 = 2, 2 = "f", 1 = 1};; val strange = true : bool