1 / 37

PROGRAMMING IN HASKELL

A recap of typeclasses and an introduction to modules in Haskell programming. Includes examples of defining types, implementing typeclasses, and using modules.

jrandolph
Download Presentation

PROGRAMMING IN HASKELL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PROGRAMMING IN HASKELL Types and Modules Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)

  2. Recap of Typeclasses We have seen typeclasses, which describe classes of data where operations of a certain type make sense. Look more closely at a new example: class Eq a where (==) :: a -> a -> Bool (/=) :: a -> a -> Bool x == y = not (x /= y) x /= y = not (x == y) 1

  3. Now – say we want to make a new type and make sure it belongs to a given typeclass. Here’s how: data TrafficLight = Red | Yellow | Green instance EqTrafficLight where Red == Red = True Green == Green = True Yellow == Yellow = True _ == _ = False 2

  4. Now maybe we want to be able to display these at the prompt. To do this, we need to add this to the “show” class. (Remember those weird errors with the trees yesterday? We hadn’t added trees to this class!) instance Show TrafficLight where show Red = "Red light" show Yellow = "Yellow light" show Green = "Green light" 3

  5. And finally, we can use these things: ghci> Red == Red True ghci> Red == Yellow False ghci> Red `elem` [Red, Yellow, Green] True ghci> [Red, Yellow, Green] [Red light,Yellowlight,Green light] 4

  6. + 1  2 3 Arithmetic Expressions Consider a simple form of expressions built up from integers using addition and multiplication.

  7. Using recursion, a suitable new type to represent such expressions can be declared by: data Expr = Val Int | Add ExprExpr | MulExprExpr For example, the expression on the previous slide would be represented as follows: Add (Val 1) (Mul (Val 2) (Val 3))

  8. Using recursion, it is now easy to define functions that process expressions. For example: size :: ExprInt size (Val n) = 1 size (Add x y) = size x + size y size (Mul x y) = size x + size y eval :: ExprInt eval (Val n) = n eval (Add x y) = eval x + eval y eval (Mul x y) = eval x * eval y

  9. Note: • The three constructors have types: Val :: Int  Expr Add :: Expr  Expr  Expr Mul :: Expr  Expr  Expr • Many functions on expressions can be defined by replacing the constructors by other functions using a suitable fold function. For example: eval = fold id (+) (*)

  10. Exercise: Edit our simple expressions to support subtraction and division in eval as well. You’ll probably need this: data Expr = Val Int | Add ExprExpr | MulExprExpr deriving Show eval :: ExprInt eval (Val n) = n eval (Add x y) = eval x + eval y eval (Mul x y) = eval x * evaly Add (Val 1) (Mul (Val 2) (Val 3))

  11. 5 7 3 6 9 1 4 Binary Trees In computing, it is often useful to store data in a two-way branching structure or binary tree.

  12. Using recursion, a suitable new type to represent such binary trees can be declared by: data Tree = Leaf Int | Node Tree Int Tree For example, the tree on the previous slide would be represented as follows: Node (Node (Leaf 1) 3 (Leaf 4)) 5 (Node (Leaf 6) 7 (Leaf 9))

  13. We can now define a function that decides if a given integer occurs in a binary tree: occurs :: Int Tree Bool occurs m (Leaf n) = m==n occurs m (Node l n r) = m==n || occurs m l || occurs m r But… in the worst case, when the integer does not occur, this function traverses the entire tree.

  14. Now consider the function flatten that returns the list of all the integers contained in a tree: flatten :: Tree  [Int] flatten (Leaf n) = [n] flatten (Node l n r) = flatten l ++ [n] ++ flatten r A tree is a search tree if it flattens to a list that is ordered. Our example tree is a search tree, as it flattens to the ordered list [1,3,4,5,6,7,9].

  15. Search trees have the important property that when trying to find a value in a tree we can always decide which of the two sub-trees it may occur in: occurs m (Leaf n) = m==n occurs m (Node l n r) | m==n = True | m<n = occurs m l | m>n = occurs m r This new definition is more efficient, because it only traverses one path down the tree.

  16. Exercise Node (Node (Leaf 1) 3 (Leaf 4)) 5 (Node (Leaf 6) 7 (Leaf 9)) A binary tree is complete if the two sub-trees of every node are of equal size. Define a function that decides if a binary tree is complete. data Tree = Leaf Int | Node Tree Int Tree occurs :: Int Tree Bool occurs m (Leaf n) = m==n occurs m (Node l n r) = m==n || occurs m l || occurs m r

  17. Modules So far, we’ve been using built-in functions provided in the Haskell prelude. This is a subset of a larger library that is provided with any installation of Haskell. (Google for Hoogle to see a handy search engine for these.) Examples of other modules: - lists - concurrent programming - complex numbers - char - sets - …

  18. Example: Data.List To load a module, we need to import it: import Data.List All the functions in this module are immediately available: numUniques::(Eqa)=>[a]->Int numUniques=length.nub This is a function in Data.List that removes duplicates from a list. function concatenation

  19. You can also load modules from the command prompt: ghci>:m+Data.List Or several at once: ghci>:m+Data.ListData.MapData.Set Or import only some, or all but some: importData.List(nub,sort) importData.Listhiding(nub)

  20. If duplication of names is an issue, can extend the namespace: importqualifiedData.Map This imports the functions, but we have to use Data.Map to use them – like Data.Map.filter. When the Data.Map gets a bit long, we can provide an alias: importqualifiedData.MapasM And now we can just type M.filter, and the normal list filter will just be filter.

  21. Data.List has a lot more functionality than we’ve seen. A few examples: ghci>intersperse'.'"MONKEY" "M.O.N.K.E.Y" ghci>intersperse0[1,2,3,4,5,6] [1,0,2,0,3,0,4,0,5,0,6] ghci>intercalate""["hey","there","guys"] "heythereguys" ghci>intercalate[0,0,0][[1,2,3],[4,5,6], [7,8,9]] [1,2,3,0,0,0,4,5,6,0,0,0,7,8,9] 20

  22. And even more: ghci>transpose[[1,2,3],[4,5,6], [7,8,9]] [[1,4,7],[2,5,8],[3,6,9]] ghci>transpose["hey","there","guys"]["htg","ehu","yey","rs","e"] ghci>concat["foo","bar","car"] "foobarcar" ghci>concat[[3,4,5],[2,3,4],[2,1,1]] [3,4,5,2,3,4,2,1,1] 21

  23. And even more: ghci>and$map(>4)[5,6,7,8] True ghci>and$map(==4)[4,4,4,3,4] False ghci>any(==4)[2,3,5,6,1,4] True ghci>all(>4)[6,9,10] True 22

  24. A nice example: adding functions Functions are often represented as vectors: 8x^3 + 5x^2 + x - 1 is [8,5,1,-1]. So we can easily use List functions to add these vectors: ghci>mapsum$transpose[[0,3,5,9], [10,0,0,9],[8,5,1,-1]] [18,8,6,17] 23

  25. There are a ton of these functions, so I could spend all semester covering just lists. More examples: group, sort, dropWhile, takeWhile, partition, isPrefixOf, find, findIndex, delete, words, insert,… Instead, I’ll make sure to post a link to a good overview of lists on the webpage, in case you need them. In essence, if it’s a useful thing to do to a list, Haskell probably supports it! 24

  26. The Data.Char module: includes a lot of useful functions that will look similar to python, actually. Examples: isAlpha, isLower, isSpace, isDigit, isPunctuation,… ghci>allisAlphaNum"bobby283" True ghci>allisAlphaNum"eddythefish!"False ghci>groupBy((==)`on`isSpace) "heyguysitsme" ["hey","","guys","","its","","me"] 25

  27. The Data.Char module has a datatype that is a set of comparisons on characters. There is a function called generalCategory that returns the information. (This is a bit like the Ordering type for numbers, which returns LT, EQ, or GT.) ghci>generalCategory'' Space ghci>generalCategory'A' UppercaseLetter ghci>generalCategory'a' LowercaseLetter ghci>generalCategory'.' OtherPunctuation ghci>generalCategory'9' DecimalNumber ghci>mapgeneralCategory" ¥t¥nA9?|" [Space,Control,Control,UppercaseLetter,DecimalNumber,OtherPunctuation,MathSymbol]] 26

  28. There are also functions that can convert between Ints and Chars: ghci>mapdigitToInt"FF85AB" [15,15,8,5,10,11] ghci>intToDigit15 'f' ghci>intToDigit5 '5' ghci>chr97 'a' ghci>mapord"abcdefgh" [97,98,99,100,101,102,103,104] 27

  29. Neat application: Ceasar ciphers A primitive encryption cipher which encodes messages by shifted them a fixed amount in the alphabet. Example: hello with shift of 3 encode::Int->String->String encodeshiftmsg= letords=mapordmsg shifted=map(+shift)ords inmapchrshifted 28

  30. Now to use it: ghci>encode3"Heeeeey" "Khhhhh|" ghci>encode4"Heeeeey" "Liiiii}" ghci>encode1"abcd" "bcde" ghci>encode5"MarryChristmas!Hohoho!” "Rfww~%Hmwnxyrfx&%Mt%mt%mt&" 29

  31. Decoding just reverses the encoding: decode::Int->String->String decodeshiftmsg= encode(negateshift)msg ghci>encode3"Imalittleteapot" "Lp#d#olwwoh#whdsrw" ghci>decode3"Lp#d#olwwoh#whdsrw" "Imalittleteapot" ghci>decode5.encode5$"Thisisasentence" "Thisisasentence" 30

  32. Making our own modules We specify our own modules at the beginning of a file. For example, if we had a set of geometry functions: moduleGeometry (sphereVolume ,sphereArea ,cubeVolume ,cubeArea ,cuboidArea ,cuboidVolume )where

  33. Then, we put the functions that the module uses: sphereVolume::Float->Float sphereVolumeradius=(4.0/3.0)*pi* (radius^3) sphereArea::Float->Float sphereArearadius=4*pi*(radius^2) cubeVolume::Float->Float cubeVolumeside=cuboidVolumesidesideside … 32

  34. Note that we can have “private” helper functions, also: cuboidVolume::Float->Float->Float ->Float cuboidVolumeabc=rectangleAreaab*c cuboidArea::Float->Float-> Float->Float cuboidAreaabc=rectangleAreaab*2+rectangleAreaac*2+rectangleAreacb*2 rectangleArea::Float->Float->Float rectangleAreaab=a*b 33

  35. Can also nest these. Make a folder called Geometry, with 3 files inside it: • Sphere.hs • Cubiod.hs • Cube.hs • Each will hold a separate group of functions. • To load: import Geometry.Sphere Or (if functions have same names): import qualified Geometry.Sphere as Sphere 34

  36. The modules: moduleGeometry.Sphere (volume ,area )where volume::Float->Float volumeradius=(4.0/3.0)*pi*(radius^3) area::Float->Float arearadius=4*pi*(radius^2) 35

  37. moduleGeometry.Cuboid (volume ,area )where volume::Float->Float->Float->Float volumeabc=rectangleAreaab*c …   36

More Related