introduction to haskell ii
play

Introduction to Haskell II Rolf Fagerberg Spring 2005 1 - PowerPoint PPT Presentation

Introduction to Haskell II Rolf Fagerberg Spring 2005 1


  1. Introduction to Haskell II Rolf Fagerberg Spring 2005 1

  2. ✞ ✝ ✄ ☎ ✆ ✆ ✝ ✂ ✟ � ✂ ✞ ☎ ✆ ✆ ✂ ✄ ☛ ✠ ✠ ☞ � ✆ ✠ � ✄ ✁ ☞ � � ✆ ✄ ✂ � ✄ ☎ ☎ ✆ ✆ ✂ ✡ Operators Operators = built-in set of functions with short non-letter names. Examples: (addition), (subtraction), (list concatenation). Most have two parameters and are written using infix notation: ← infix ← usual prefix notation for functions We can convert between “operator” and “standard” version of two parameter functions ❀ Def: ❀ ❀ 2

  3. ☛ ✂ ✁ ✂ ✡ ✡ ✡ ☎ ☎ ✁ ✁ ☛ ✄ ✁ ✂ ☛ ☎ � ☛ ✂ ✄ ✁ ☛ ✄ ✄ ✄ ✡ ✄ ✂ ✡ ✄ ☎ ✂ ✂ ✄ ✄ ✄ ✂ ☛ ☎ ✁ ☛ � ✄ ✂ ✁ ✡ � ☛ ✄ � ✂ ✡ ✡ ✂ ✁ ✄ ✂ ✁ � ✄ � ✂ ✄ ✡ ✂ � � ✂ ✡ ✡ ✡ ☛ ☎ � ✂ ✄ ☛ � ✂ ☛ � ☛ ☛ ✂ ✄ ✡ Associativity and Binding Power To save on parentheses, operators (along with function application) are given diffent binding powers : = To resolve evaluation order of sequences of operators of equal binding power, they have an associativity assigned: = = = So and are left associative , whereas is right associative . 3

  4. ☎ ✂ ✄ ✝ ✁ ✁ ✂ ✁ � ✄ � � � ✄ Do-it-yourself operators You can define new operators (see Appendix C for rules). Example: Minimum operator: (??) :: Int -> Int -> Int x ?? y | x > y = y | otherwise = x Now: ❀ Define associativiy and binding power: 4

  5. ✡ ✝ ☞ ✌ ☎ ✝ ✑ ✟ ✔ ☎ ✡ ✡ ✌ ✑ ✒ ☎ ✝ ✟ ☎ ☎ ✝ ✌ ✝ ✌ ☛ ✟ ✁ ☛ ✟ ✂ ☎ ✝ ✟ ✂ ✝ ☎ ✝ ✡ ✑ ✟ ✑ ☛ ✟ ✎ ☎ ✡ ✑ ✡ ✓ ✔ ✖ ✟ ✁ ✏ ☎ ✒ ✡ ✝ ✝ ☎ ✏ ☛ ✟ ✝ � ✏ ☎ ✒ ✝ ✒ ✏ ✑ ✓ ✟ ✎ ☎ ✄ ✏ ✝ ✏ ☎ ✒ ✓ ✄ ✔ ✕ ✂ ✄ ✖ ✁ ✗ ✕ ✂ ✑ ✝ ✏ ☛ ✖ ✡ ✔ ✞ ☛ ✟ ✡ ✒ ✝ ✡ ✁ ✂ ☛ ✝ ✝ ✡ ✁ ✟ ✞ ☛ ✂ ✂ ☎ ✁ ✁ ☎ � ✁ ✟ ☎ ✁ ☎ � ✂ ✟ ☎ � ✁ ✡ ✂ ✁ ☎ ☛ � ✂ ✁ ✄ ✟ ☎ ✝ ✁ ☎ ✄ ✝ ✡ ✂ ☛ ✝ ✝ ✡ ✟ ✟ ✑ ☛ ✟ ✟ ☎ ✝ ✡ ✂ ☛ ✎ ☛ ✝ ✟ ✡ ✂ ☛ ✝ ✝ ✡ ✏ ✁ ☛ ✘ ✞ ☎ ✝ ✡ ✂ ☛ ✝ ✝ ✡ ✖ ✏ 5 Definitions may use pattern matching on the parameters: ❀ ☞✍✌ ☞✍✌ Pattern Matching ❀ ☞✍✌ ✞ ✠✟ ✄ ✞✏ ✄ ✞✏ ☞✍✌ ☞✍✌ ☞✍✌ ☞✍✌ ✝ ✠✟ ✝ ✠✟ ✝ ✠✟ ✄ ✞✝ ✄✆☎

  6. ✝ ✝ ✝ ✓ ✁ ☛ � ✡ ✝ ✓ ✏ ✏ ☛ ✡ ✓ ✡ ✂ ✄ ✁ ✂ ✟ ✡ ☛ ✝ � ☛ ✂ ✂ ☛ ☛ ☎ ✝ ✁ ✏ ✑ � ✟ ✔ ✖ ✂ ✝ ✡ ✞ ✟ ✡ Pattern Matching A pattern is made of: • Literals , , , ☞✍✌ • Identifiers , (wild card is a nameless variable) • Tuple constructor ✝ ✠✟ ✞ ✠✟ • List constructor • More constructors later. . . A pattern can be hierarchical: A pattern can match or fail. To match, all sub-patterns must recursively match. When a match occurs, any matched identifiers are bound to the value matched. 6

  7. ✝ ✓ ✔ ✡ ☎ ✟ � ☛ ✖ ✁ ✁ ✂ ✡ ✝ ✝ ✁ ✏ ☛ ✡ ✞ ✓ ✞ ✏ ☛ ✟ ✡ � ✞ ☛ ✗ ✖ ✁ ☎ ☛ ✓ ✝ ✏ ☛ ✟ ✝ � � � ✡ ✂ � ✄ � ✝ ✏ ✁ ✁ ✂ ✓ ✓ ✔ ☎ ✖ ✁ ✗ ✔ ✓ ✁ ✄ � ☎ ✟ ✂ ✟ ✄ ✖ ✔ � ☎ � ✟ � � ✂ ✖ � ✔ ✡ ☎ ✟ � ☎ � ☛ ✟ ✡ ✂ ✔ ✁ ✂ ✔ ✝ ✏ ✞ ✏ ✁ ✁ ✂ ✡ ✝ ✓ ✝ ✏ ☛ ✖ ✁ ✟ ✔ ✖ ✁ ✁ ✂ ✔ ✖ ✁ ✏ ✟ ✔ ✖ ✡ ☎ � ☛ ✖ � ✡ ✂ � ☎ ✄ ✡ ✝ ✓ ✝ ✏ ✟ ✟ ✝ � � � ✡ ✂ � ☎ ✄ ✝ ✏ � ✡ ✔ ✖ � ✖ � ✡ ✂ � ☎ ✄ ✓ ✓ ✔ ✔ ✕ ✂ ✄ ✖ ✔ ✁ ✗ ✔ ✕ ✂ ✄ ✖ � ✡ ✂ � ☎ ✄ ✂ ☎ � ✖ ✄ ✓ ✓ ✔ ✔ ☎ ✖ ✖ ✁ ✗ ✔ ☎ � � ✡ ✂ � ☎ ✄ ✔ ✖ ✟ ✔ ✖ � ✡ ✂ ☎ ✂ ✄ ✖ ✔ ✔ ☎ ✟ ✂ ✖ ✟ ✔ ✂ ✟ ✠ ✟ ✘ ✖ ✡ ✖ ✔ ☎ ✟ ✂ ✟ ✂ ✟ ✠ ✟ ✘ ✖ � ✟ 7 Polymorphism ❀ ❀ ✝ ✠✟ Types can be parametric

  8. ✒ ☎ ☎ ✔ ✗ ✁ ☛ � ✗ ✁ ✡ ✁ ✓ ✓ ✂ ☎ ☛ ☎ ✌ ✆ ✖ ✗ ✡ ✔ ✓ ✝ ✡ ✁ ✂ ☎ ✒ ✖ ✟ ✔ ✖ ✔ ✁ ✂ ☎ ✒ ✖ � ✄ ✁ ✏ ✁ ✂ ✁ ✁ ✒ ✌ ✝ ✄ ✄ ✁ ✁ ☎ ✂ ☎ ✒ ✂ ✁ ✝ ✏ � ✄ ✟ ✆ ☎ ✄ ✆ ✄ ✡ ✁ ✁ ✌ ✄ ✄ ✡ ✁ ✝ ✄ ✆ ✄ ✡ ✁ ✓ ✝ Functions as parameters and results In Haskell, functions are values (value ∼ expression trees with empty leaves). Can be passed to and from functions (then called high-order functions). Very useful high-order functions: , , , , , , 8

  9. ✆ ☎ ✂ ☎ ✆ ✆ ☎ ☛ � ☎ ✡ ✟ ✝ ✂ ✆ ☛ ✆ ☎ ☎ ☎ ✆ ✆ ☎ ✟ ✝ ✂ ☎ ✆ ✝ ✄ ☎ ✖ ☎ ✒ ✟ ✄ ✄ ☛ ✝ ✂ ☎ ✆ ✆ ☎ ✄ ✄ ✂ ✕ ✔ ✗ ✁ ✖ ✄ ✂ ✕ ✔ ✓ ✓ ✆ ✄ ✡ ✁ ✗ ✁ ✄ ✂ ✕ ✓ ✓ ✆ ✆ ☎ ✆ ✁ ✁ ✂ ✟ ✁ ✝ � ✁ ☎ ✄ ✁ � ✟ ✁ ☛ ✕ ✄ ✂ ✆ ✕ ✗ ✁ ✄ ✂ ✕ ✓ ✓ ✝ ✂ ☎ ✆ ☎ ✁ ✞ � ✝ ✟ ✞ ✝ ✆ ✆ ☎ ✄ ✂ ✕ ✗ ✂ 9 Functions as parameters and results • Partial application (currying): Generating functions as results: or • Composition: ✄ ✄✂

  10. � ☎ ✄ ☎ ✏ ✂ ✝ ✄ ✄ � ✄ ✡ ✌ ✝ ✡ ✁ ✂ ✌ ✝ ✄ ☎ ✆ ✝ ✁ ✒ ✁ ✄ ✂ ✄ ✄ ✄ ✄ ✂ ✓ � � ✂ ✂ ✁ ✁ � ✏ ✁ ☎ ☎ ✁ ✂ ✄ � ✁ ☎ ✄ Some Library Functions in Prelude Check A Tour of the Haskell Prelude See 10

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend