Download presentation

Presentation is loading. Please wait.

Published byBarbara Santry Modified over 3 years ago

1
Haskell Chapter 6

2
Modules A module defines some functions, types, and type classes A program is a collection of modules Module used in GHCi is Prelude (you can import into your own files, to get the same environment) Programming Language issues: How well does the language help programmers organize their code (to support larger projects) How does the language support/encourage reuse? Accessing functionality: challenging for novice programmers (remember getting linker errors?)

3
Importing imports must be before function definitions, normally at the top of the file Each import statement goes on a separate line Ex: import Data.List import Data.List (nub, sort) What if you have your own function? import Data.List hiding (nub) What if same function (e.g., filter) in 2 modules? import qualified Data.Map then must specify Data.Map.filter Must do for every function from module – tedious! so: import qualified Data.Map as M now do M.filter

4
More on modules and imports To find useful functions: http://www.haskell.org/hoogle/ http://www.haskell.org/hoogle/ In GHCi, add imports :m + Data.List :m + Data.List Data.Map Data.Set After load file :browse to get a list of functions

5
Some Examples Use Data.List functions to count how many times each word appears in a string Use Data.List functions to see if one list is wholly contained in another Use Data.Char functions to encode using a Caesar cipher Book has more

6
Count Words Uses words, group, and sort from Data.List import Data.List -- function composition (.) in chapter 5 wordNums :: String -> [(String, Int)] wordNums = map (\ws -> (head ws, length ws)). group. sort. Words wordNums' :: String -> [(String, Int)] wordNums' str = map (\ws -> (head ws, length ws)) (group (sort (words str))) Quick Ex: Play with the individual functions on this slide and the next two… Figure out what words, group, sort, isPrefixOf, any, tails, or and chr do Example: type words "hi ho hi ho to work I go" nothing to submit

7
Needle in Haystack Uses tails, isPrefixOf and any from Data.List isIn :: (Eq a) => [a] -> [a] -> Bool needle `isIn` haystack = any (needle `isPrefixOf`) (tails haystack)

8
Caesar Cipher Uses ord and chr from Data.Char encode :: Int -> String -> String encode offset msg = map (\c -> chr $ ord c + offset) msg Or use composition encode' :: Int -> String -> String encode' offset msg = map (chr. (+ offset). ord) msg Can decode as: decode :: Int -> String -> String decode shift msg = encode (negate shift) msg

9
Create your own Modules module moduleName ( list of functions to export) may also include “helper” functions that are not exported must be in the same folder as the module that’s importing it

10
Example module Geometry ( sphereVolume, sphereArea, cubeVolume, cubeArea, cuboidArea, cuboidVolume,) where sphereVolume :: Float -> Float sphereVolume radius = (4.0 / 3.0) * pi * (radius ^3) sphereArea :: Float -> Float sphereArea radius = 4 * pi * (radius ^ 2) cubeVolume :: Float -> Float cubeVolume side = cuboidVolume side side side cubeArea :: Float -> Float cubeArea side = cuboidArea side side side cuboidVolume :: Float -> Float -> Float -> Float cuboidVolume a b c = rectArea a b * c cuboidArea :: Float -> Float -> Float -> Float cuboidArea a b c = rectArea a b * 2 + rectArea a c * 2 + rectArea c b * 2 rectArea :: Float -> Float -> Float rectArea a b = a * b

11
Modules can also be hierarchical Sphere.hs Cuboid.hs module Geometry.Sphere ( volume, area ) where volume :: Float -> Float volume radius = (4.0 / 3.0) * pi * (radius ^3) area :: Float -> Float area radius = 4 * pi * (radius ^ 2) module Geometry.Cuboid ( volume, area ) where volume :: Float -> Float -> Float -> Float volume a b c = rectArea a b * c area :: Float -> Float -> Float -> Float area a b c = rectArea a b * 2 + rectArea a c * 2 + rectArea c b * 2 rectArea :: Float -> Float -> Float rectArea a b = a * b

12
Hierarchical modules continued Cube.hs Using module Geometry.Cube ( volume, area ) where import qualified Geometry.Cuboid as Cuboid volume :: Float -> Float volume side = Cuboid.volume side side side area :: Float -> Float area side = Cuboid.area side side side import qualified Geometry.Sphere as Sphere import qualified Geometry.Cuboid as Cuboid import qualified Geometry.Cube as Cube sphereDemo radius = "The area of a sphere with radius " ++ show radius ++ " is " ++ show (Sphere.area radius)

Similar presentations

OK

Modeling with Haskell Scientific Seminar 03/04 Gerhard Navratil.

Modeling with Haskell Scientific Seminar 03/04 Gerhard Navratil.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To ensure the functioning of the site, we use **cookies**. We share information about your activities on the site with our partners and Google partners: social networks and companies engaged in advertising and web analytics. For more information, see the Privacy Policy and Google Privacy & Terms.
Your consent to our cookies if you continue to use this website.

Ads by Google

Ppt on edgeworth box Ppt on computer malware anti-malware Ppt on taj mahal hotel Ppt on william harvey experiments on blood circulation Ppt on solar system in hindi Ppt on congruence of triangles free download Ppt on power system stability theory Ppt on harvard business review Presentation ppt on motivational Ppt on 3 idiots learning