Season 1 아카이브/프로그래밍
                
              Functional Programming with Haskell - Loading functions from a file
                문장전달자
                 2018. 3. 30. 18:55
              
              
                    
        728x90
    
    
  함수의 정의를 파일에 넣어서 사용할 수 있다. 이 경우 let 은 불필요하다.
simple.hs 파일에 아래와 같이 4개의 함수를 입력하고 저장하자.
.hs 확장자는 반드시 필요하다.
simple.hs
1 2 3 4  | double x = x * x ::Integer neg x = -x::Integer isPositive x = x > (0::Integer) toCelsius temp = (tem[ - 32) * 5/(9::Double)  | cs | 
다음과 같이 커맨드라인에서 해당 파일의 내용을 확인해보자.
1 2 3 4 5  | (blackbriar) root@gcloudx:~/blackbriar# cat simple.hs double x = x * x ::Integer neg x = -x::Integer isPositive x = x > (0::Integer) toCelsius temp = (tem[ - 32) * 5/(9::Double)  | cs | 
simple.hs 가 현재 디렉토리 상에 위치한다고 가정하면, 다음과 같이 :load 를 이용해서 함수를 로딩할 수 있다. 이 경우 확장자 hs 는 넣을 필요가 없다. 그리고 :browse 를 통해서 로딩된 내용을 확인할 수 있다. 물론 :load ~/372/hs/simple 와 같이 경로를 넣는 것도 가능하다.
1 2 3 4 5  | Prelude> :load simple [1 of 1] Compiling Main             ( simple.hs, interpreted ) Ok, modules loaded: Main. *Main>  | cs | 
1 2 3 4 5 6 7 8  | *Main> :browse double :: Integer -> Integer neg :: Integer -> Integer isPositive :: Integer -> Bool toCelsius :: Double -> Double *Main>  | cs | 
아래와 같이 :load 대신 :l 만으로도 로딩이 가능하다. 그리고 로딩 이후에는 :reload 로 충분하다.
1 2 3 4 5 6 7 8 9 10  | *Main> :l simple [1 of 1] Compiling Main             ( simple.hs, interpreted ) Ok, modules loaded: Main. *Main> :reload Ok, modules loaded: Main. *Main>  | cs | 
728x90