`

haskell - modules - define your own modules

阅读更多

this is an additional post called "haskell - modules - import and common modules". 

 

We've looked at some cool modules so far, but how do we make our own module? Almost every programming language enables you to split your code up into several files and Haskell is no different. When making programs, it's good practice to take functions and types that work towards a similar purpose and put them in a module. That way, you can easily reuse those functions in other programs by just importing your module.

 

It is a typical examples, where you define a Geometry module, and its function is shared by modules such as Cuboid, Cube adn Sphere. 

you will normally do this to export function of modules to outsides.

module Geometry  
( sphereVolume  
, sphereArea  
, cubeVolume  
, cubeArea  
, cuboidArea  
, cuboidVolume  
) where  

 

 

first it is the READMe.txt file, it has the following contents.

--+ README
module is a collection of related functions, types and teypclasses.. 

Having code split up into several modules has quite a lot of advantages. If a module is generic enough, the functions it exports can be used in a multitude of different programs. 

--+ resources

http://learnyouahaskell.com/modules


--+ modules
1). first we introduce the Geometry.hs file, which is a glob class with every function in it
  - module load 
      import Geometry
2). then we organize the Geometry.hs into hierarchical.structure 
  - module load 
      import Geometry.Sphere
    or 
      import Geometry.Sphere as Sphere
      import Geometry.Cuboid as Cuboid
      import Geometry.Cube as Cube

 

 

and the definition of the Geometry module. 

-- file
--   Geometry.hs
-- description:
--   making your own modules

-- 
-- module $module_name 
-- (
-- exported_symbol1, 
-- exported_symbol2,
-- exported_symbol3,
-- exported_symbol4,
-- ) where
-- 
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 = cuboidArea side side side

cubeArea :: Float -> Float  
cubeArea side = cuboidArea side side side  
  
cuboidVolume :: Float -> Float -> Float -> Float  
cuboidVolume a b c = rectangleArea a b * c  
  
cuboidArea :: Float -> Float -> Float -> Float  
cuboidArea a b c = rectangleArea a b * 2 + rectangleArea a c * 2 + rectangleArea c b * 2  
  
rectangleArea :: Float -> Float -> Float  
rectangleArea a b = a * b

 Where we have created a sub-directory where we put sub-modules like Sphere, Cuboid, Cube and others. 

 

 

-- file:
--  Cuboid.hs
-- description:
--  define the Geometry.Cuboid module

module Geometry.Cuboid
( volume
, area
) where

volume :: Float -> Float -> Float -> Float  
volume a b c = rectangleArea a b * c  
  
area :: Float -> Float -> Float -> Float  
area a b c = rectangleArea a b * 2 + rectangleArea a c * 2 + rectangleArea c b * 2  
  
rectangleArea :: Float -> Float -> Float  
rectangleArea a b = a * b  

 

 

and then we have Cube module, which has the following contents.

( 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 

 

 

then following is the Sphere module. 

 

-- file: 
--  Cube.hs
-- description:
--  define the Geometry.Cube module
--

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 

 

then you might want to write some Module_Main module where you can write some test function there. 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics