{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

324-lecture13

# 324-lecture13 - Principles of Programming Languages...

This preview shows pages 1–7. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Principles of Programming Languages Principles of Programming Languages Lecture 13 Lecture 13 University of Toronto Wael Aboulsaadat Wael Aboulsaadat [email protected] [email protected] http://portal.utoronto.ca/ 1 ML: pattern matching ML: pattern matching – cont’d cont’d • Patterns may consist of constants (integers , true, false..) , tuples and variables. Arithmetic or logical expressions are invalid. – E.g fun wrong(x=y) = “…” • No duplicates in patterns – E.g. fun wrong_equal (x,y) = true | wrong_equal (x,y) = false; University of Toronto • Pattern matching with wild cards – E.g. fun first (x,_) = x; – Matches anything like a variable. Binds nothing. – Avoid need to name every pattern • ML does extensive pattern checking – E.g.- fun reverse (h::t) = reverse(t) @ [h]; > Warning: match nonexhaustive 2 ML: pattern matching ML: pattern matching – cont’d cont’d • How ML matches patterns? fun func (x::y::zs,w) = | (…) = | (…) = func([1,2,3,4],5) (,) :: 5 :: 1 (,) w :: :: x University of Toronto :: :: nil 4 3 2 y zs 3 ML: record type ML: record type • We have seen list [ ] and tuple ( )… • Record syntax { <field 1 >:<type 1 >, <field 2 >:<type 2 >,….,<field n >:<type n >} • A record instance is defined as { <label >=<value >, <label >=<value >,….,<label >=<value >} University of Toronto 1 1 2 2 n n • A record is a structured data type in which each element is accessed by a unique name. – E.g. { name: string, age: int, salary: int} – {name = “Dave”, age = 77, salary=99000} > val it = {name = “Dave”, age = 77, salary=99000} : {name:string, age:int,salary:int} A field called name with type string A field called called age with type int A field called salary with type int 4 ML: record type ML: record type – cont’d cont’d • Operations – # operator to extract a field from a record instance – E.g.- #salary {name =“john”, age=35, salary=90}; > val it = 90 : int- #options {startcity="toronto",endcity="boston", University of Toronto options=("12",10,"K")}; > val it = ("12",10,"K") : string * int * string 5 ML: record type ML: record type – cont’d cont’d • Named Types – ML provides a way to give a name to a type • E.g- type waitress = { name: string , wages: int, tips: int }; – Named types can be used anywhere that ML types can be. • E.g.- fun income (w: waitress) = #wages w + #tips w; > val income = fn : waitress -> int University of Toronto- fun income (w: { name: string , wages: int, tips: int }) = #wages w + #tips w; > val income = fn : {name:string, tips:int, wages:int} -> int – Named types can be used in type declaration • E.g.- type waitresses = waitress list;- [{name=“sally”, wages= 20, tips=10}, {name=“alice”, wages= 15, tips=15}] 6 ML: record type...
View Full Document

{[ snackBarMessage ]}

### Page1 / 20

324-lecture13 - Principles of Programming Languages...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online