290 likes | 432 Views
Programming Language Concepts (CIS 635). Elsa L Gunter 4303 GITC NJIT, www.cs.njit.edu/~elsa/635. 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 www.cs.njit.edu/~elsa/635/SML_Lect/sml-shell
E N D
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT, www.cs.njit.edu/~elsa/635
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 www.cs.njit.edu/~elsa/635/SML_Lect/sml-shell • For the SML code for today’s lecture see www.cs.njit.edu/~elsa/635/SML_Lect/intro.sml
WWW Addresses for SML • http://www.cs.njit.edu/~elsa/635/110-smlnj.exe • ftp://ftp.research.bell-labs.com/dist/smlnj/release/110/110-smlnj.exe • http://cm.bell-labs.com/cm/cs/what/smlnj/index.html • http://cm.bell-labs.com/cm/cs/what/smlnj/doc/basis/pages/sml-std-basis.html
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
Session in SML Standard ML of New Jersey, Version 110.0.7, September 28, 2000 [CM&CMB] - (* Read-eval-print loop; expressions and declarations *) - 2 + 3; val it = 5 : int - val test = 3 < 2; val test = false : bool
SML Expressions and Declarations - (* = At top-level, an expression = <exp>; = is treated as an abbreviation for the declaration = val it = <exp>; =*) - "Hi there"; val it = "Hi there" : string
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
Overloading for Basic Arithmetic - val x = 5 + 7; val x = 12 : int - val y = x * 2; val y = 24 : int - val z = 1.35 + 0.23; val z = 1.58 : real
Overloading but No Coercion - val w = y + z; stdIn:51.1-51.14 Error: operator and operand don't agree [tycon mismatch] operator domain: int * int operand: int * real in expression: y + z
Using SML Code From a File • File named test.sml contains 3 + 2; val x = 5 + 7; val y = x * 2; val z = 1.35 + 0.23; val w = y + z;
Compiler Output - 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 A:\SML_Lect\test.sml:5.1-5.14 Error: operator and operand don't agree [tycon mismatch] operator domain: int * int operand: int * real in expression: y + z
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
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
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
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
Values fixed at declaration time - val x = 7; val x = 7 : int - plus_x 3; val it = 15 : int
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
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
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
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 *)
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
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
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
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}
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
Records and Tuples - val q = (280,{student = {Name = "Joseph Martins", = ss = (325,40,1276), = age = 19}, = instructor = teacher});
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}}
Tuples are Records - val strange = (1,"f",2) = {3 = 2, 2 = "f", 1 = 1};; val strange = true : bool