Lec2 Crash
Lec2 Crash
ML Flow
Expressions (Syntax) Compile-time Static Exec-time Dynamic Values (Semantics)
Types
Unbounded size Can have lists of anything (e.g. lists of lists) But
5
Question 1
Which of these causes a type error? (a) [1; 2; 3] (b) [1, 2, 3] (c) [1; 2; 3] (d) (1, 2, 3) (e) [1; 2; 3]
7
::
[1] [1;2;3] [a;b;c]
int list int list string list
Lists: Construct
Nil operator
[] []:a list [] => []
Cons operator
1::[2;3]
int list
[1;2;3]
@
[1;2;3;4;5] [a;b] [1]
int list string list int list
1 @ [2;3];
[1] @ [a;b];
10
hd
1 a
int string
hd [];
11
tl
[2;3] [b]
int list string list
tl [];
12
(a) Syntax Error (b) true : bool (c) false : bool (d) Type Error (hd) (e) Type Error (=)
13
Lists: Deconstruct
Head Tail
e :T list hd e : T e => v1::v2 hd e => v1
e :T list tl e : T list
e 1: T e 2 : T e1=e2 : bool
string list
14
(3, abcd,(3.5,4.2))
triple = 3 elts
Lists:
Same type, unbounded number:
[3;4;5;6;7] int list
Syntax:
Tuples = comma Lists = semicolon
15
16
Branches
17
18
19
If-then-else expressions
e1 : bool e2: T e3: T if e1 then e2 else e3 : T
(if 1>2 then [1,2] else [])=(if 1<2 then [] else [a])
20
If-then-else expressions
if (1 < 2) then [1;2] else 5 if false then [1;2] else 5
e1
21
Variables
22
let x = e;;
Bind the value of expression
to the variable
25
Local bindings
for expressions using temporary variables
let tempVar = x + 2 * y in tempVar * tempVar ;;
17424
int
tempVar is bound only inside expr body from in Not visible (not in scope) outside
27
Binding by Pattern-Matching
Simultaneously bind several variables
# let (x,y,z) = (2+3,a^b, 1::[2]);; val x : int = 5 val y : string = ab val z : int list = [1;2]
29
Binding by Pattern-Matching
But what of:
# let h::t = [1;2;3];; Warning P: this pattern-matching not exhaustive. val h : int = 1 val t : int list = [2;3]
Why is it whining ?
# let h::t = []; Exception: Match_failure # let XS = [1;2;3]; val xs = [1;2;3]: list - val h::t = xs; Warning: Binding not exhaustive val h = 1 : int val t = [2;3] : int
In general
Binding by Pattern-Matching
But what of:
# let h::t = [1;2;3];; NEVER USE PATTERN MATCHING Warning P: this pattern-matching not exhaustive. val h : int = 1 LIKE THIS val t : int list = [2;3]
Why is it whining ?
In general
# let h::t = []; Exception: Match_failure ALWAYS USE THIS FORM INSTEAD # let XS = [1;2;3]; val xs = [1;2;3]: list - val h::t = xs; match l with ... Warning: Binding not exhaustive val h = 1 : int val (coming t = [2;3] int but this is important) up:soon,
Functions
32
Type
Everything is an expression Everything has a value Everything has a type
A function is a value!
33
34
A Problem
Parameter (formal) Body Expr fn int -> int
How a call (application) is evaluated: 1. Evaluate argument 2. Bind formal to arg value 3. Evaluate Body expr
35
How a call (application) is evaluated: 1. Evaluate argument 2. Bind formal to arg value 3. Evaluate Body expr
36
39
41
val filter : (a->bool)->a list->a lisi) = fn # let list1 = [1;31;12;4;7;2;10];; # filter is5lt list1 ;; val it : int list = [31;12;7;10] # filter is5gte list1;; val it : int list = [1;4;2] # filter even list1;; val it : int list = [12;4;2;10]
42
# let partition f l = (filter f l, filter (neg f) l); val partition :(a->bool)->a list->a list * a list = fn
# let list1 = [1,31,12,4,7,2,10]; - # partition is5lt list1 ; val it : (int list * int list) = ([31,12,7,10],[1,2,10] # partition even list1; val it : (int list * int list) = ([12,4,2,10],[1,31,7])
43
A little trick
# 2 <= 3;;
val it : bool = true # ba <= ab;; val it : bool = false # let lt = (<) ;; val it : a -> a -> bool = fn # lt 2 3;; val it : bool = true; # lt ba ab ;; val it : bool = false;
# let is5Lt = lt 5; val is5lt : int -> bool = fn; # is5lt 10; val it : bool = true; # is5lt 2; val it : bool = false;
44