如何在Haskell中导入.hs文件

如何在Haskell中导入.hs文件,haskell,syntax,module,Haskell,Syntax,Module,我制作了一个名为time.hs的文件。它包含一个函数,用于测量另一个函数的执行时间 有没有办法将time.hs文件导入另一个Haskell脚本 我想要像这样的东西: module Main where import C:\Haskell\time.hs main = do putStrLn "Starting..." time $ print answer putStrLn "Done." module time where Import <necessary

我制作了一个名为
time.hs
的文件。它包含一个函数,用于测量另一个函数的执行时间

有没有办法将
time.hs
文件导入另一个Haskell脚本

我想要像这样的东西:

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

main = do
    putStrLn "Starting..."
    time $ print answer
    putStrLn "Done."
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
时间在“time.hs”中定义为:

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

main = do
    putStrLn "Starting..."
    time $ print answer
    putStrLn "Done."
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
模块时间,其中
进口
时间a=do
开始时间

module Time where
...
import Time
...
script.hs

module Time where
...
import Time
...
命令行:

ghc --make script.hs

如果模块
Time.hs
与“主”模块位于同一目录中,您只需键入:

导入时间
可以使用层次结构,这样您就可以编写
import Utils.Time
。 据我所知,你想用的方法是行不通的


有关模块的更多信息,请参见此处。

假设我在同一目录中有两个文件:ModuleA.hs和ModuleB.hs

模块a.hs:

module ModuleA where
...
模块B.hs:

module ModuleB where

import ModuleA
...
我可以这样做:

    ghc -I. --make ModuleB.hs
注意:模块名和文件名必须相同。否则它无法编译。差不多

找不到模块“…”


将发生。

我了解到,在本例中,
script.hs
必须位于您当前的工作目录中。这意味着您不能执行ghc--make path/to/script.hs
,例如。