tags:

views:

703

answers:

2

Hi I have made on file called time.hs which contains a single function for measuring the time another function takes to complete.

Is there a way to import this time.hs file into the other haskell scripts I have made similar to import?

I sort of want:

module Main where
import C:\Haskell\time.hs

main = do
    putStrLn "Starting..."
    time $ print answer
    putStrLn "Done."

Where time is defined in the time.hs file by:

module time where
Import <necessary modules>

time a = do
start <- getCPUTime
v <- a
end   <- getCPUTime
let diff = (fromIntegral (end - start)) / (10^12)
printf "Computation time: %0.3f sec\n" (diff :: Double)
return v

But I don't know how to import or load a seperate .hs file, is this possible or must I compile the time.hs file into a module?

+10  A: 

Time.hs:

module Time where
...

script.hs:

import Time
...

Command line:

ghc --make script.hs
yairchu
+3  A: 

If the module time.hs is located in the same directory as your "main" module, you can simply type:

import time

It is possible to use a hierchical structure, so that you can write import utils.time. As far as I know, the way you want to do it won't work.

For more information on modules, see here: http://learnyouahaskell.com/modules#making-our-own-modules

Christian