* 꼬리 재귀호출과 패턴 매칭을 이용하여 구현한 팩토리얼과 피보나치 수열 계산

{-
    Filename: fact.hs
         Rapid factorial and fibonacci seq implementations
         by pattern matching and tail recursive call

    Compile: ghc fact.hs
    Execute: main

    Date: 2010/07/20
    Author: phkim  pkim (AT) scripts.pe.kr
-}

module Main where

  factorial :: Integer -> Integer
  factorial n =  recfact 1 n

  recfact :: Integer -> Integer -> Integer
  recfact acc k = case k of
        0 -> acc
        k-> recfact (acc*k) (k - 1)

  fib :: Integer -> Integer
  fib n = fibGen 0 1 n

  fibGen :: Integer -> Integer -> Integer -> Integer
  fibGen a b n = case n of
        0 -> a
        n -> fibGen b (a + b) (n - 1)

  main :: IO ()
  main = do
        print ("Factorial(30000) has " ++ show (length (show (factorial 30000))) ++ " digits")
        print ("Fibonacci(200000) has " ++ show (length (show (fib 200000))) ++ " digits")

{-
    Expected result:
    "Factorial(30000) has 121288 digits"
    "Fibonacci(200000) has 41798 digits"
-}



크리에이티브 커먼즈 라이선스
Creative Commons License

 

Posted by Scripter
,

*파일명: 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
,

Haskell 언어로 구구단 중의 2단을 출력하는 프로그램 소스를 작성해 보았다.
Haskell 언어는 순수한 함수형 언어이기 때문에 절차적 언어에서 많이 쓰는 for 반복문을 지원하지 않는다.


  아래의 소스에서는 리스트 자료형을 적절히 사용하였다.

ex는 정수들로 구성된 리스트를 참조하는 변수로, dan은 정수 2를 의미하는 상수로 사용되었다.

es는 구구단의 단을 의미하는 정수(여기서는 2)를 인자로 받아서 출력될 스트링의 리스트를 만드는 함수이다. 'show 인자'는  인자(여기서는 정수)를 스트링으로 변환하는 일을 한다.

printDan은 세 함수 map,  concat , putStr를 합성한 함수로서  스트링의 리스트를
인자로 받아 리스트의 각 요소(스트링) 마다 새줄 문자 "\n"를 붙여서 컨솔에 출력하는 일을 한다. 소스에서 점(.)은 함수의 합성 연산자이다.



소스 파일명: Gugudan.hs
------------------------------[소스 시작]
ex :: [Int]
ex = [1, 2 .. 9]

es :: Int -> [[Char]]
es dd = [ show dd ++ " * " ++ show x ++ " = " ++ show (dd*x) | x <- ex ]

printDan :: [[Char]] -> IO ()
printDan = putStr . concat . map (++"\n")

dan :: Int
dan = 2

main = do printDan (es dan)
------------------------------[소스 끝]


WinHugs의 runhugs.exe를 이용하여 스크립트 소스파일 Gugudan.hs을 실행해 보았다.

실행> runhugs Gugudan.hs
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18



WinHugs의 대화형 인터프리터 hugs.exe를 이용하여 스크립트 소스파일 Gugudan.hs을 실행해 보았다. 마지막에 :quit는 인터프리터를 종료하는 명령이다. 즉, Ctrl+Z 키를 누른 경우와 같다.


실행> hugs
__   __ __  __  ____   ___      _________________________________________
||   || ||  || ||  || ||__      Hugs 98: Based on the Haskell 98 standard
||___|| ||__|| ||__||  __||     Copyright (c) 1994-2005
||---||         ___||           World Wide Web: http://haskell.org/hugs
||   ||                         Bugs: http://hackage.haskell.org/trac/hugs
||   || Version: 20051031       _________________________________________

Haskell 98 mode: Restart with command line option -98 to enable extensions

Type :? for help
Hugs> :load Gugudan.hs
Main> main
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18

Main> :quit
[Leaving Hugs]






WinHugs의 GUI 대화형 인터프리터 winhugs.exe를 이용하여 스크립트 소스파일 Gugudan.hs을 실행해 보았다. :cd는 change directory 명령이고 :load는 소스파일 탑재(load) 명령이다.
소스파일이 탑재되고 나면 프롬프트가 Main> 으로 바뀐다. 이는 소스파일에 main 함수가 작성되어 있기 때문이다.


<wingugs.exe를 이용하여 Gugudan.hs를 실행시킨 장면>






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


Posted by Scripter
,