07 - Click to edit Master subtitle style Chapter Seven...

Info iconThis preview shows pages 1–9. 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
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 Seven Modern Programming Languages, 2nd ed. A Second Look At ML Chapter Seven Modern Programming Languages, 2nd ed. 11 Chapter Seven Modern Programming Languages, 2nd ed. Outline Patterns Local variable definitions A sorting example Chapter Seven Modern Programming Languages, 2nd ed. 22 Chapter Seven Modern Programming Languages, 2nd ed. Two Patterns You Already Know We have seen that ML functions take a single parameter: fun f n = n*n; We have also seen how to specify functions with more than one input by using tuples: fun f (a, b) = a*b; Both n and (a, b) are patterns . The n matches and binds to any argument, while (a,b) matches any 2-tuple and binds a and b to its components Chapter Seven Modern Programming Languages, 2nd ed. 33 Chapter Seven Modern Programming Languages, 2nd ed. Underscore As A Pattern The underscore can be used as a pattern It matches anything, but does not bind it to a variable Preferred to: fun f x = "yes"; Chapter Seven Modern Programming Languages, 2nd ed. 44- fun f _ = "yes"; val f = fn : 'a -> string- f 34.5; val it = "yes" : string- f ; val it = "yes" : string Chapter Seven Modern Programming Languages, 2nd ed. Constants As Patterns Any constant of an equality type can be used as a pattern But not: fun f 0.0 = "yes"; Chapter Seven Modern Programming Languages, 2nd ed. 55- fun f 0 = "yes"; Warning: match nonexhaustive 0 => ... val f = fn : int -> string- f 0; val it = "yes" : string Chapter Seven Modern Programming Languages, 2nd ed. Non-Exhaustive Match In that last example, the type of f was int -> string , but with a match non- exhaustive warning Meaning: f was defined using a pattern that didnt cover all the domain type ( int ) So you may get runtime errors like this: Chapter Seven Modern Programming Languages, 2nd ed. 66- f 0; val it = "yes" : string- f 1; uncaught exception nonexhaustive match failure Chapter Seven Modern Programming Languages, 2nd ed. Lists Of Patterns As Patterns You can use a list of patterns as a pattern This example matches any list of length 2 It treats a and _ as sub-patterns, binding a to the first list element Chapter Seven Modern Programming Languages, 2nd ed. 77- fun f [a,_] = a; Warning: match nonexhaustive a :: _ :: nil => ... val f = fn : 'a list -> 'a- f [#"f",#"g"]; val it = #"f" : char Chapter Seven Modern Programming Languages, 2nd ed. Cons Of Patterns As A Pattern You can use a cons of patterns as a pattern x::xs matches any non-empty list, and binds x to the head and xs to the tail Parens around x::xs are for precedence Chapter Seven Modern Programming Languages, 2nd ed....
View Full Document

Page1 / 38

07 - Click to edit Master subtitle style Chapter Seven...

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

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