1 / 52

PROGRAMMING IN HASKELL

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

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 Modules, I/O and functors Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)

  2. 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 - …

  3. 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

  4. 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)

  5. 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.

  6. 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] 5

  7. 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] 6

  8. 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 7

  9. 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] 8

  10. 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! 9

  11. 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"] 10

  12. 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]] 11

  13. 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] 12

  14. 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 13

  15. 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&" 14

  16. 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" 15

  17. 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

  18. 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 … 17

  19. 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 18

  20. 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 19

  21. 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) 20

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

  23. File I/O So far, we’ve worked mainly at the prompt, and done very little true input or output. This is logical in a functional language, since nothing has side effects! However, this is a problem with I/O, since the whole point is to take input (and hence change some value) and then output something (which requires changing the state of the screen or other I/O device. Luckily, Haskell offers work-arounds that separate the more imperative I/O.

  24. A simple example: save the following file as helloword.hs main=putStrLn"hello,world" Now we actually compile a program: $ghc--makehelloworld [1of1]CompilingMain (helloworld.hs,helloworld.o) Linkinghelloworld... $./helloworld hello,world 23

  25. What are these functions? ghci>:tputStrLn putStrLn::String->IO() ghci>:tputStrLn"hello,world" putStrLn"hello,world"::IO() So putStrLn takes a string and returns an I/O action (which has a result type of (), the empty tuple). In Haskell, an I/O action is one with a side effect - usually either reading or printing. Usually some kind of a return value, where () is a dummy value for no return. 24

  26. An I/O action will only be performed when you give it the name “main” and then run the program. A more interesting example: main=do putStrLn"Hello,what'syourname?” name<-getLine putStrLn("Hey"++name++", yourock!") Notice the do statement - more imperative style. Each step is an I/O action, and these glue together. 25

  27. More on getLine: ghci>:tgetLine getLine::IOString This is the first I/O we’ve seen that doesn’t have an empty tuple type - it has a String. Once the string is returned, we use the <- to bind the result to the specified identifier. Notice this is the first non-functional action we’ve seen, since this function will NOT have the same value every time it is run! This is called “impure” code, and the value name is “tainted”. 26

  28. An invalid example: nameTag="Hello,mynameis"++getLine What’s the problem? Well, ++ requires both parameters to have the same type. What is the return type of getLine? Another word of warning: what does the following do? name=getLine 27

  29. Just remember that I/O actions are only performed in a few possible places: • A main function • inside a bigger I/O block that we have composed with a do (and remember that the last action can’t be bound to a name, since that is the one that is the return type). • At the ghci prompt: ghci>putStrLn"HEEY" HEEY 28

  30. You can use let statements inside do blocks, to call other functions (and with no “in” part required): importData.Char main=do putStrLn"What'syourfirstname?" firstName<-getLine putStrLn"What'syourlastname?" lastName<-getLine letbigFirstName=maptoUpperfirstName bigLastName=maptoUpperlastName putStrLn$"hey"++bigFirstName++""++ bigLastName++",howareyou?" Note that <- is for I/O, and let for expressions. 29

  31. What is return? Does NOT signal the end of execution! Return instead makes an I/O action out of a pure value. main=do a<-return"heck" b<-return"yeah!" putStrLn$a++""++b In essence, return is the opposite of <-. Instead of “unwrapping” I/O Strings, it wraps them. 30

  32. Last example was a bit redundant, though – could use a let instead: main = do let a = ”heck" b = "yeah" putStrLn $ a ++ " " ++ b   Usually, you’ll use return to create I/O actions that don’t do anything (but you have to have one anyway, like an if-then-else), or for the last line of a do block, so it returns some value we want. 31

  33. Takeaway: Return in haskellis NOT like other languages. main=do line<-getLine ifnullline thenreturn() elsedo putStrLn$reverseWordsline main reverseWords::String->String reverseWords=unwords. mapreverse.words Note: reverseWords = unwords . map reverse . words is the same as reverseWordsst = nwords (map reverse (words st)) 32

  34. Other I/O functions: • print (works on any type in show, but calls show first) • putStr - And as putStrLn, but no newline • putChar and getChar main=doprintTrue print2 print"haha" print3.2 print[3,4,3] main=do c<-getChar ifc/='' thendo putCharc main elsereturn() 33

  35. More advanced functionality is available in Control.Monad: importControl.Monad importData.Char main=forever$do putStr"Givemesomeinput:" l<-getLine putStrLn$maptoUpperl (Will indefinitely ask for input and print it back out capitalized.) 34

  36. Other functions: • sequence: takes list of I/O actions and does them one after the other • mapM: takes a function (which returns an I/O) and maps it over a list • Others available in Control.Monad: • when: takes boolean and I/O action. If bool is true, returns same I/O, and if false, does a return instead 35

  37. System Level programming • Scripting functionality deals with I/O as a necessity. • The module System.Environment has several to help with this: • getArgs: returns a list of the arguments that the program was run with • getProgName: returns the string which is the program name (Note: I’ll be assuming you compile using “ghc –make myprogram” and then running “./myprogram”. But you could also do “runhaskell myprogram.hs”.) 36

  38. An example: import System.Environment import Data.List main = do args <- getArgs progName <- getProgName putStrLn "The arguments are:" mapMputStrLnargs putStrLn "The program name is:" putStrLnprogName 37

  39. The output: $ ./arg-test first second w00t "multi word arg" The arguments are: first second w00t multi word arg The program name is: arg-test 38

  40. Functors Functors are a typeclass, just like Ord, Eq, Show, and all the others. This one is designed to hold things that can be mapped over; for example, lists are part of this typeclass. classFunctorfwhere fmap::(a->b)->fa->fb This type is interesting - not like previous exmaples, like in EQ, where (==) :: (Eq a) => a -> a -> Bool. Here, f is NOT a concrete type, but a type constructor that takes one parameter. 39

  41. Compare fmap to map: fmap::(a->b)->fa->fb map :: (a -> b) -> [a] -> [b] So map is a lot like a functor! Here, map takes a function and a list of type a, and returns a list of type b. In fact, can define map in terms of fmap: instanceFunctor[]where fmap=map 40

  42. Notice what we wrote: instanceFunctor[]where fmap=map We did NOT write “instance Functor [a] where…”, since f has to be a type constructor that takes one type. Here, [a] is already a concrete type, while [] is a type constructor that takes one type and can produce many types, like [Int], [String], [[Int]], etc. 41

  43. Another example: instanceFunctorMaybewhere fmapf(Justx)=Just(fx) fmapfNothing=Nothing Again, we did NOT write “instance Functor (Maybe m) where…”, since functor wants a type constructor. Mentally replace the f’s with Maybe, so fmap acts like (a -> b) -> Maybe a -> Maybe b. If we put (Maybe m), would have (a -> b) -> (Maybe m) a -> (Maybe m) b, which looks wrong. 42

  44. Using it: ghci>fmap(++"HEYGUYSIMINSIDETHE JUST")(Just"Somethingserious.") Just"Somethingserious.HEYGUYSIMINSIDETHEJUST" ghci>fmap(++"HEYGUYSIMINSIDETHE JUST")Nothing Nothing ghci>fmap(*2)(Just200) Just400 ghci>fmap(*2)Nothing Nothing 43

  45. Back to trees, as an example to put it all together: Let’s make a binary search tree type. Need comparisons to make sense, so want the type to be in Eq. Also going to have it be Show and Read, so anything in the tree can be converted to a string and printed (just to make displaying easier). data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show,Read,Eq) 44

  46. Note that this is slightly different than our last class. Node 5 (Node 3 (Node 1 EmptyTreeEmptyTree) (Node 4 EmptyTreeEmptyTree)) (Node 6 EmptyTreeEmptyTree) This will let us code an insert that’s a bit easier to process, though! First step – a function to make a single node tree: singleton::a->Treea singletonx=NodexEmptyTreeEmptyTree 45

  47. Now – go code insert! treeInsert::(Orda)=>a->Treea->Treea treeInsertxEmptyTree= treeInsertx(Nodealeftright)  =  46

  48. My insert: treeInsert::(Orda)=>a->Treea->Treea treeInsertxEmptyTree=singletonx treeInsertx(Nodealeftright) |x==a=Nodexleftright |x<a=Nodea(treeInsertxleft)right |x>a=Nodealeft(treeInsertxright) 47

  49. Find: findInTree::(Orda)=>a->Treea->Bool findInTreexEmptyTree=False findInTreex(Nodealeftright) |x==a=True |x<a=findInTreexleft |x>a= findInTree xright Note: This is a binary search tree, so can be efficient. If it were an “unordered” tree, like we saw last week, would need to search both left and right subtrees. 48

  50. An example run: ghci>letnums=[8,6,4,1,7,3,5] ghci>letnumsTree=foldrtreeInsertEmptyTreenums ghci>numsTree Node5(Node3(Node1EmptyTreeEmptyTree)(Node4EmptyTreeEmptyTree))(Node7(Node6EmptyTreeEmptyTree)(Node8EmptyTreeEmptyTree)) 49

More Related