*파일명: 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
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
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
'프로그래밍 > Haskell' 카테고리의 다른 글
Haskell 언에에서 형(type) 변환하기 (0) | 2009.10.19 |
---|---|
if...else... 조건문 사용 예제 for Haskell (0) | 2009.10.19 |
거듭제곱(지수) 계산 with Haskell (0) | 2009.10.18 |
구구단 출력 예제 with Haskell (0) | 2009.05.29 |
Hello 예제 with Haskell (0) | 2009.05.26 |