Download presentation
Presentation is loading. Please wait.
Published byLaura Heath Modified over 9 years ago
1
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT, www.cs.njit.edu/~elsa/635 www.cs.njit.edu/~elsa/635
2
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 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
3
Copyright 2002 Elsa L. Gunter 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.exeftp://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.htmlhttp://cm.bell- labs.com/cm/cs/what/smlnj/doc/basis/pages/sml-std- basis.html
4
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
5
Copyright 2002 Elsa L. Gunter 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
6
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
7
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
8
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 = 1.35 + 0.23; val z = 1.58 : real
9
Copyright 2002 Elsa L. Gunter 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
10
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 = 1.35 + 0.23; val w = y + z;
11
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
12
Copyright 2002 Elsa L. Gunter 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 Compiler Output
13
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
14
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
15
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
16
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
17
Copyright 2002 Elsa L. Gunter Values fixed at declaration time - val x = 7; val x = 7 : int - plus_x 3; val it = 15 : int
18
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
19
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
20
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
21
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 *)
22
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
23
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
24
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
25
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}
26
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
27
Copyright 2002 Elsa L. Gunter Records and Tuples - val q = (280,{student = {Name = "Joseph Martins", = ss = (325,40,1276), = age = 19}, = instructor = teacher});
28
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}}
29
Copyright 2002 Elsa L. Gunter Tuples are Records - val strange = (1,"f",2) = {3 = 2, 2 = "f", 1 = 1};; val strange = true : bool
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.