Crash Course in ML - Programming Language - Lecture Slides, Slides of Computer Engineering and Programming

In this short course we study the basic concept of the programming. In these lecture slides the key points are:Crash Course in Ml, Complex Types, Nonempty List, List Operator, Type of Resulting Expression, Variables and Bindings, Undeclared Variables, Local Bindings, Binding by Pattern-Matching, Simultaneous Binding

Typology: Slides

2012/2013

Uploaded on 04/23/2013

sarasvatir
sarasvatir 🇮🇳

4.5

(28)

86 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE 130: Spring 2012
Programming Languages
Lecture 2: A Crash Course in ML
1
Wednesday, April 4, 2012
News
On webpage:
Suggested HW #1
PA #1 (due next Fri 4/13)
Please post questions to Piazza
Today: A crash course in ML contd…
2
Wednesday, April 4, 2012
Recap: MLs Holy Trinity
1. Programmer enters expression
2. ML checks if expression is “well-typed”
Using a precise set of rules, ML tries to find a unique
type for the expression meaningful type for the expr
3. ML evaluates expression to compute value
Of the same “type” found in step 2
Expressions (Syntax) Values (Semantics)
Types
Compile-time
“Static”
Exec-time
“Dynamic”
3
Wednesday, April 4, 2012
Complex types: Lists
Unbounded size
Can have lists of anything (e.g. lists of lists)
But…
4
Wednesday, April 4, 2012
Docsity.com
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Crash Course in ML - Programming Language - Lecture Slides and more Slides Computer Engineering and Programming in PDF only on Docsity!

CSE 130: Spring 2012

Programming Languages

Lecture 2: A Crash Course in ML

Wednesday, April 4, 2012 1 News

On webpage:

  • Suggested HW #
  • PA #1 (due next Fri 4/13)

Please post questions to Piazza

Today: A crash course in ML contd…

Wednesday, April 4, 2012 2 Recap: ML’s Holy Trinity

1. Programmer enters expression

2. ML checks if expression is “well-typed”

  • Using a precise set of rules, ML tries to find a unique
type for the expression meaningful type for the expr

3. ML evaluates expression to compute value

  • Of the same “type” found in step 2
Expressions (Syntax) Values (Semantics)
Types
Compile-time
“Static”
Exec-time
“Dynamic”

Complex types: Lists []; (^) [] ’a list

  • Unbounded size
  • Can have lists of anything (e.g. lists of lists)
  • But… [1;2;3]; [1;2;3] int list [“a”;“b”; “c”^“d”]; [“a”;“b”; “cd”] string list [1+1;2+2;3+3;4+4]; [2;4;6;8] int list [(1,“a”^“b”);(3+4,“c”)]; [(1,“ab”);(7,“c”)] (int*string) list [[1];[2;3];[4;5;6]]; (^) [[1];[2;3];[4;5;6]]; (int list) list

Docsity.com

Complex types: Lists

All elements must have same type

[1; “pq”];

Wednesday, April 4, 2012 5 Complex types: Lists List operator “Cons” [1] int list 1::[2;3]; (^) [1;2;3] int list 1::[“b”; “cd”]; “a”::[“b”;“c”]; [“a”;“b”;“c”]^ string list

1::[];

Can only “cons” element to a list of same type Wednesday, April 4, 2012 6 Lists: Construct

[] [] : ’a list [] => []

1::[2;3] [1;2;3]

int list

Cons operator e1=>v1 e2 => v e1::e2 => v1::v e1 : T e2 : T list e1::e2 : T list Nil operator Complex types: Lists List operator “Append” int list [“a”]@[“b”]; (^) [“a”;“b”] string list 1 @ [2;3]; []@[1]; [1] int list

[1;2]@[3;4;5];

Can only append two lists

[1;2;3;4;5]

… of the same type^ [1]^ @^ [“a”;“b”];

Docsity.com

So far, a fancy calculator… … what do we need next? Wednesday, April 4, 2012 13 So far, a fancy calculator…

Branches

Wednesday, April 4, 2012 14 If-then-else expressions

  • Then-subexp, Else-subexp must have same type!
    • Equals type of resulting expression if 1>2 then [1,2] else [] (^) [] int list if 1<2 then [] else [“a”] (^) [] string list (if 1>2 then [1,2] else [])=(if 1<2 then [] else [“a”]) e1 : bool e2: T e3: T if e1 then e2 else e3 : T If-then-else expressions if (1 < 2) then [1;2] else 5 e1 : bool e2: T e3: T if e1 then e2 else e3 : T if false then [1;2] else 5
  • then-subexp, else-subexp must have same type!
  • …which is the type of resulting expression

Docsity.com

So far, a fancy calculator…

Variables

Wednesday, April 4, 2012 17 Variables and bindings

let x = e;

“Bind the value of expression e to the variable x”

# let x = 2+2;;

val x : int = 4

Wednesday, April 4, 2012 18

# let x = 2+2;;
val x : int = 4
# let y = x * x * x;;
val y : int = 64
# let z = [x;y;x+y];;
val z : int list = [4;64;68]

Variables and bindings

Later declared expressions can use x

  • Most recent “bound” value used for evaluation

    let p = a + 1;

    Characters 8-9: let p = a + 1 ;; ^ Unbound value a Variables and bindings Undeclared variables (i.e. without a value binding) are not accepted! Catches many bugs due to typos

Docsity.com

Next class: functions, but remember …

Everything is an expression
Everything has a value
Everything has a type

Expression (^) Value Type

A function is a value!

Wednesday, April 4, 2012 25 Complex types: Functions!

fun x -> x+1;; fn
int -> int
Parameter
(formal)
Body
Expr

let inc = fun x -> x+1 ;

val inc : int -> int = fn

inc 0;

val it : int = 1

inc 10;

val it : int = 11 Wednesday, April 4, 2012 26 A Problem

fn
int -> int
Parameter
(formal)
Body
Expr

Functions only have

ONE parameter ?!

How a call (“application”) is evaluated:

  1. Evaluate argument
  2. Bind formal to arg value
  3. Evaluate “Body expr”
fun x -> x+1;;

A Solution: Simultaneous Binding

fun (x,y) -> x bool
Parameter
(formal)
Body
Expr

How a call (“application”) is evaluated:

  1. Evaluate argument
  2. Bind formal to arg value
  3. Evaluate “Body expr”

Functions only have

ONE parameter?

Docsity.com

Another Solution (“Currying”)

fun x -> fun y-> x (int -> bool)
Parameter
(formal)
Body
Expr

Whoa! A function can return a function

let lt = fun x -> fun y -> x < y ;

val lt : int -> int -> bool = fn

let is5Lt = lt 5;

val is5lt : int -> bool = fn;

is5lt 10;

val it : bool = true;

is5lt 2;

val it : bool = false; Wednesday, April 4, 2012 29 And how about…

fun f -> fun x -> not(f x); fn
(’a ->bool) -> (’a -> bool)
Parameter
(formal)
Body
Expr

A function can also take a function argument

let neg = fun f -> fun x -> not (f x);

val lt : int -> int -> bool = fn

let is5gte = neg is5lt;

val is5gte : int -> bool = fn

is5gte 10;

val it : bool = false;

is5gte 2;

val it : bool = true; (…odd, even …) Wednesday, April 4, 2012 30

A shorthand for function binding

# let neg = fun f -> fun x -> not ( f x );

# let neg f x = not ( f x );
val neg : int -> int -> bool = fn
# let is5gte = neg is5lt;
val is5gte : int -> bool = fn;
# is5gte 10;
val it : bool = false;
# is5gte 2;
val it : bool = true;

Put it together: a “filter” function

  • let rec filter f xs = match xs with | [] -> [] | (x::xs’)-> if f x then x::(filter f xs’) else (filter f xs’);; val filter : (‘a->bool)->‘a list->‘a lisi) = fn
If arg “matches”
this pattern…
…then use
this Body Expr

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]

Docsity.com