소스 파일명: testIf.py

  1. {-#
  2.    Filename: testIf.hs
  3.    Purpose:  Example using the conditional control structure syntax
  4.                  if .... else ... then ...
  5.    Execute: runhugs testIf.hs [number]
  6.          Or
  7.             runhaskell testIf.hs [number]
  8. #-}
  9. module Main where
  10. import System.Environment
  11. -- 사용법을 보여주는 함수
  12. printUsing :: IO ()
  13. printUsing = do
  14.     putStrLn "Using: runhugs testIf.hs [number]"
  15.     putStrLn "This determines whether the number is positive or not."
  16. main :: IO ()
  17. main = do
  18.     args <- getArgs
  19.     -- 명령행 인자의 개수가 1이 아니면 사용법을 보여준다.
  20.     if not ((length args) == 1)
  21.         then printUsing
  22.         else do
  23.             let val = toDouble (args !! 0)
  24.             if val > 0.0
  25.                then putStrLn ((show val) ++ " is a positive number.")
  26.                else if val < 0.0
  27.                    then putStrLn ((show val) ++ " is a negative number.")
  28.                    else putStrLn ((show val) ++ " is zero.")
  29. toDouble :: [Char] -> Double
  30. toDouble x = (read x) :: Double

 


위의 소스에서
            if not ((length args) == 1)
로 된 부부은
            if not ((length args) == 1)
로 해도 된다. 즉, Haskell 언어의 /=  비교연산자는 C 언어의 != 비교연산자와 같은 의미이다.

실행> runhugs testIf.hs
Using: runhugs testIf.hs [number]
This determines whether the number is positive or not.

실행> runhugs testIf.hs 6.25
6.25 is a positive number.

실행> runhugs testIf.hs -6.25
-6.25 is a negative number.



* GHC의 ghc 명령으로 컴파일한 후 실행하기

컴파일> ghc --make testIf.hs

testIf.hs:1:0: Unrecognised pragma
[1 of 1] Compiling Main             ( testIf.hs, testIf.o )

testIf.hs:1:0: Unrecognised pragma
Linking testIf.exe ...

실행> testIf
Using: runhugs testIf.hs [number]
This determines whether the number is positive or not.

실행> testIf 6.25
6.25 is a positive number.

실행> testIf -6.25
-6.25 is a negative number.




Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.


Posted by Scripter
,