This preview shows pages 1–10. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full 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 Higherorder functions and currying Predefined higherorder functions Chapter Nine Modern Programming Languages, 2nd ed. 22 Chapter Nine Modern Programming Languages, 2nd ed. More PatternMatching Last time we saw patternmatching in function definitions: fun f 0 = "zero"  f _ = "nonzero"; Patternmatching occurs in several other kinds of ML expressions: case n of 0 => "zero"  _ => "nonzero"; 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 righthand 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 < caseexpr > ::= 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 Higherorder functions and currying Predefined higherorder 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
 Spring '11
 Hamlen

Click to edit the document details