120 likes | 300 Views
Haskell. Chapter 6. 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:
E N D
Haskell Chapter 6
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?)
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
More on modules and imports • To find useful functions: • http://www.haskell.org/hoogle/ • In GHCi, add imports • :m + Data.List • :m + Data.ListData.MapData.Set • :browse to get a list of modules
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
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
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)
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
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
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 sideside cubeArea :: Float -> Float cubeArea side = cuboidArea side sideside 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
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
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 sideside area :: Float -> Float area side = Cuboid.area side sideside 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)