program story

Haskell에서 .hs 파일을 가져 오는 방법

inputbox 2020. 12. 31. 08:14
반응형

Haskell에서 .hs 파일을 가져 오는 방법


라는 파일을 만들었습니다 time.hs. 다른 함수의 실행 시간을 측정하는 단일 함수가 포함되어 있습니다.

time.hs파일을 다른 Haskell 스크립트로 가져 오는 방법이 있습니까?

나는 다음과 같은 것을 원한다.

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

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

시간은 'time.hs'에서 다음과 같이 정의됩니다.

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

별도의 .hs파일 을 가져 오거나로드하는 방법을 모르겠습니다 . time.hs가져 오기 전에 파일을 모듈로 컴파일해야 합니까?


Time.hs:

module Time where
...

script.hs:

import Time
...

명령 줄 :

ghc --make script.hs

모듈 time.hs이 "기본"모듈과 동일한 디렉토리에있는 경우 다음을 입력하면됩니다.

import Time

계층 구조를 사용할 수 있으므로 import Utils.Time. 내가 아는 한, 당신이 원하는 방식은 작동하지 않습니다.

모듈에 대한 자세한 내용은 여기에서 Learn You a Haskell, Making Our Own Modules를 참조하십시오 .


동일한 디렉토리에 ModuleA.hs와 ModuleB.hs라는 두 개의 파일이 있다고 가정합니다.

ModuleA.hs :

module ModuleA where
...

ModuleB.hs :

module ModuleB where

import ModuleA
...

나는 이것을 할 수있다 :

    ghc -I. --make ModuleB.hs

참고 : 모듈 이름과 파일 이름은 동일해야합니다. 그렇지 않으면 컴파일 할 수 없습니다. 같은 것

모듈 '...'을 찾을 수 없습니다.

일어날 것이다.

참조 URL : https://stackoverflow.com/questions/1438446/how-to-import-a-hs-file-in-haskell

반응형