09 - Click to edit Master subtitle style Chapter Nine...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Click to edit Master subtitle style Chapter Nine Modern Programming Languages, 2nd ed. A Third Look At ML Chapter Nine Modern Programming Languages, 2nd ed. 11 Chapter Nine Modern Programming Languages, 2nd ed. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order functions Chapter Nine Modern Programming Languages, 2nd ed. 22 Chapter Nine Modern Programming Languages, 2nd ed. More Pattern-Matching Last time we saw pattern-matching in function definitions: fun f 0 = "zero" | f _ = "non-zero"; Pattern-matching occurs in several other kinds of ML expressions: case n of 0 => "zero" | _ => "non-zero"; Chapter Nine Modern Programming Languages, 2nd ed. 33 Chapter Nine Modern Programming Languages, 2nd ed. Match Syntax A rule is a piece of ML syntax that looks like this: A match consists of one or more rules separated by a vertical bar, like this: Each rule in a match must have the same type of expression on the right-hand side A match is not an expression by itself, but forms a part of several kinds of ML expressions Chapter Nine Modern Programming Languages, 2nd ed. 44 < rule > ::= < pattern > => < expression > < match > ::= < rule > | < rule > ' | ' < match > Chapter Nine Modern Programming Languages, 2nd ed. Case Expressions The syntax is This is a very powerful case constructunlike Chapter Nine Modern Programming Languages, 2nd ed. 55- case 1+1 of = 3 => "three" | = 2 => "two" | = _ => "hmm"; val it = "two" : string < case-expr > ::= case < expression > of < match > Chapter Nine Modern Programming Languages, 2nd ed. Example Chapter Nine Modern Programming Languages, 2nd ed. 66 case x of _::_::c::_ => c | _::b::_ => b | a::_ => a | nil => 0 The value of this expression is the third element of the list x , if it has at least three, or the second element if x has only two, or the first element if x has only one, or 0 if x is empty. Chapter Nine Modern Programming Languages, 2nd ed. Generalizes if The two expressions above are equivalent So if- then- else is really just a special case of case Chapter Nine Modern Programming Languages, 2nd ed. 77 if exp 1 then exp 2 else exp 3 case exp 1 of true => exp 2 | false => exp 3 Chapter Nine Modern Programming Languages, 2nd ed. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order functions Chapter Nine Modern Programming Languages, 2nd ed. 88 Chapter Nine Modern Programming Languages, 2nd ed. Predefined Functions When an ML language system starts, there are many predefined variables Some are bound to functions: Chapter Nine Modern Programming Languages, 2nd ed....
View Full Document

Page1 / 35

09 - Click to edit Master subtitle style Chapter Nine...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online