*파일명: sumSquares.hs
module Main where
import System.Environment

main :: IO ()
main = do
    args <- getArgs
    let arr = toDouble args
    print (arr)
    putStr "Total is "
    print (sumIt arr)

toDouble :: [[Char]] -> [Double]
toDouble xs = [(read x)**2 :: Double | x <- xs]

sumIt :: [Double] -> Double
sumIt [] = 0
sumIt [x] = x
sumIt (x:xs) = x + sumIt xs





*파일명: sumSquares2.hs
module Main where
import System.Environment

main :: IO ()
main = do
    args <- getArgs
    let arr = toDouble args
    print (arr)
    putStr "Total is "
    print (sumIt arr)

toDouble :: [[Char]] -> [Double]
toDouble xs = [square ((read x) :: Double) | x <- xs]

sumIt :: [Double] -> Double
sumIt [] = 0
sumIt [x] = x
sumIt (x:xs) = x + sumIt xs

square :: Double -> Double
square x = x*x


 

 

* WInHugs에서 runhugs로 실행하기
프롬프트> runhugs sumSquares.hs 1 2 3 4 5
[1.0,4.0,9.0,16.0,25.0]
Total is 55.0

프롬프트> runhugs sumSquares2.hs 1 2 3 4 5
[1.0,4.0,9.0,16.0,25.0]
Total is 55.0



* GHC의 runhaskell로 실행하기
프롬프트> runhaskell sumSquares2.hs 1 2 3 4 5
[1.0,4.0,9.0,16.0,25.0]
Total is 55.0

프롬프트> runhaskell sumSquares.hs 1 2 3 4 5
[1.0,4.0,9.0,16.0,25.0]
Total is 55.0



* GHC의 ghcl로 컴파일한 후 실행하기 (--make 옵션은 실행파일을 만들라는 의미의 옵션이다.)
프롬프트> ghc --make sumSquares.hs

프롬프트>sumSquares
[]
Total is 0.0

프롬프트> sumSquares 1 2 3 4 5
[1.0,4.0,9.0,16.0,25.0]
Total is 55.0


Posted by Scripter
,