user2878007
user2878007

Reputation: 419

Importing our own modules in haskell

I have the following code, and I only want to export only sphereVolume and sphereArea function from my module.

module Geometry  
    ( sphereVolume
    , sphereArea  
    ) 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 = rectangleArea a b * c  

 rectangleArea :: Float -> Float -> Float  
 rectangleArea a b = a * b  

When I write import Geometry in the ghci I get the following error

<no location info>:
Could not find module `Geometry':
  it is not a module in the current program, or in any known package

I made sure that they are in the same directory and with the same filename as the module. What am I missing here?

Upvotes: 0

Views: 269

Answers (2)

mhwombat
mhwombat

Reputation: 8126

As Franky says, you can use :l Geometry in order to work with your code in GHCi. But you can ony have one module loaded (with :l) at a time. what if you have written several modules that you want to be able to work with simultaneously? Then you need to import them.

In order to be able to import Geometry from within GHCi, you need to install it. The easiest way to do that is using cabal. Here is a guide.

Upvotes: 3

Franky
Franky

Reputation: 2376

Do not import in ghci. Just use the usual

:l Geometry

Upvotes: 2

Related Questions