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. 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值