Haskell 언어로도 (Python 언어 처럼) 복소수 게산과 분수 게산을 쉽게 할 수 있습니다,.

(1) Haskell  언어에서 복소수를 사용하려면   import Data.Complex 구문이 있어야 합니다.
    복소수의 표현은 실수부 :+ 허수부 입니다.

(2) Haskell  언어에서 분수를 사용하려면   import Data.Ratio 구문이 있어야 합니다.
    분수의 표현은 분자 % 분모 입니다.

(3) C 언어의 printf 나 Python 언어의 print 처럼 포맷 출력을 하려면 import Text.Printf 구문이 있어야 합니다.

아래에서 진한 글자체로 된 부분만 입력하고 엔터키를 누르면 됩니다.
(ghci 는 Glasgow Haskell 의 인터프리터이고,. ghc는 Glasgow Haskell 의 컴파일러입니다.)


$ ghci
GHCi, version 7.0.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
-- 몫과 나머지 구하기
Prelude> div 5 3
1
Prelude> mod 5 3
2
Prelude> divMod 5 3
(1,2)
Prelude> 5 `div` 3
1
Prelude> 5 `mod` 3
2
Prelude> 5 `divMod` 3
(1,2)
-- 분수 생성과 계산
Prelude> import Data.Ratio
Prelude Data.Ratio> 5 % 3
5 % 3
Prelude Data.Ratio> 5 % 3 * 2
10 % 3
Prelude Data.Ratio> 5 % 3 + 2
11 % 3
Prelude Data.Ratio> 5 % 3 + 2 % 3
7 % 3
-- 복소수 생성과  계산
Prelude Data.Ratio> import Data.Complex
Prelude Data.Ratio Data.Complex> 2 :+ 3
2.0 :+ 3.0
Prelude Data.Ratio Data.Complex> sqrt 2 :+ 3
1.4142135623730951 :+ 3.0
Prelude Data.Ratio Data.Complex> sqrt (2 :+ 3)
1.6741492280355401 :+ 0.8959774761298381
Prelude Data.Ratio Data.Complex> 2 :+ 3 * 2
2.0 :+ 6.0
Prelude Data.Ratio Data.Complex> 2 :+ 3 - 2

<interactive>:1:1:
    Precedence parsing error
        cannot mix `:+' [infix 6] and `-' [infixl 6] in the same infix expressio
n
Prelude Data.Ratio Data.Complex> (2 :+ 3) - 2
0.0 :+ 3.0
Prelude Data.Ratio Data.Complex> (2 :+ 3) - 0:+2

<interactive>:1:1:
    Precedence parsing error
        cannot mix `-' [infixl 6] and `:+' [infix 6] in the same infix expressio
n
Prelude Data.Ratio Data.Complex> (2 :+ 3) - (0:+2)
2.0 :+ 1.0
Prelude Data.Ratio Data.Complex> (2 :+ 3) * 0:+2

<interactive>:1:13:
    No instance for (RealFloat (Complex a0))
      arising from a use of `:+'
    Possible fix:
      add an instance declaration for (RealFloat (Complex a0))
    In the expression: (2 :+ 3) * 0 :+ 2
    In an equation for `it': it = (2 :+ 3) * 0 :+ 2
Prelude Data.Ratio Data.Complex> (2 :+ 3) * (0:+2)
(-6.0) :+ 4.0
Prelude Data.Ratio Data.Complex> conjugate 2 :+ 3

<interactive>:1:13:
    No instance for (RealFloat (Complex a0))
      arising from a use of `:+'
    Possible fix:
      add an instance declaration for (RealFloat (Complex a0))
    In the expression: conjugate 2 :+ 3
    In an equation for `it': it = conjugate 2 :+ 3
Prelude Data.Ratio Data.Complex> conjugate (2 :+ 3)
2.0 :+ (-3.0)
relude Data.Ratio Data.Complex> 1 / 2 :+ 3
.5 :+ 3.0
Prelude Data.Ratio Data.Complex> 1 / (2 :+ 3)
.15384615384615385 :+ (-0.23076923076923078)
Prelude Data.Ratio Data.Complex>
Prelude Data.Ratio Data.Complex Text.Printf> let x = 2 :+ 5
Prelude Data.Ratio Data.Complex Text.Printf> printf "x = %f + %fj\n" (realPart x) (imagPart x)
x = 2.0 + 5.0j
-- 복소수 츨력하기
Prelude Data.Ratio Data.Complex Text.Printf> let printComplex a = print ((show (
realPart a)) ++ " + " ++ (show (imagPart a)) ++ "j")
Prelude Data.Ratio Data.Complex Text.Printf> printComplex x
"2.0 + 5.0j"
Prelude Data.Ratio Data.Complex Text.Printf> printComplex (conjugate x)
"2.0 + -5.0j"
Prelude Data.Ratio Data.Complex Text.Printf> printComplex (sin x)
"67.47891523845588 + -30.879431343588244j"
Prelude Data.Ratio Data.Complex Text.Printf> printComplex (asin (sin x))
"1.1415926535897933 + -5.0j"
Prelude Data.Ratio Data.Complex Text.Printf> printComplex ((asin . sin) x)
"1.1415926535897933 + -5.0j"
-- 분수 츨력하기
Prelude Data.Ratio Data.Complex Text.Printf> let printFraction a = print ((show
(numerator a)) ++ "/" ++ (show (denominator a)))
Prelude Data.Ratio Data.Complex Text.Printf> let b = 2 % 3
Prelude Data.Ratio Data.Complex Text.Printf> printFraction b
"2/3"
Prelude Data.Ratio Data.Complex Text.Printf> printFraction (1/b)
"3/2"
Prelude Data.Ratio Data.Complex Text.Printf> printFraction ((1/b)*2)
"3/1"
-- (인용 부호 없이) 분수 츨력하기
Prelude Data.Ratio Data.Complex Text.Printf> let printFraction a = printf "%d/%d
\n" (numerator a) (denominator a)
Prelude Data.Ratio Data.Complex Text.Printf> printFraction b
2/3
Prelude Data.Ratio Data.Complex Text.Printf> printFraction (1/b)
3/2
Prelude Data.Ratio Data.Complex Text.Printf> printFraction ((1/b)*2)
3/1
-- (인용 부호 없이) 복소수 츨력하기
Prelude Data.Ratio Data.Complex Text.Printf> let printComplex a = printf "%f + %
fj\n" (realPart a) (imagPart a)
Prelude Data.Ratio Data.Complex Text.Printf> printComplex x
2.0 + 5.0j
Prelude Data.Ratio Data.Complex Text.Printf> printComplex (conjugate x)
2.0 + -5.0j
Prelude Data.Ratio Data.Complex Text.Printf> printComplex (sin x)
67.47891523845588 + -30.879431343588244j
Prelude Data.Ratio Data.Complex Text.Printf> printComplex (asin (sin x))
1.1415926535897933 + -5.0j
Prelude Data.Ratio Data.Complex Text.Printf> printComplex ((asin . sin) x)
1.1415926535897933 + -5.0j
Prelude Data.Ratio Data.Complex Text.Printf> :q
Leaving GHCi.

 

 

※ 다음은 위에 입력한 내용들을 모아서 만든 Haskell 소스 파일입니다.

{-#
      Filename: complexAndFraction.hs
          Testing the expressions and calculations of complex numbers and fractions in Haskell.

     Compile: ghc complexAndFraction.hs
     Execute: ./complexAndFraction

     Or
 
     Execute: runhaskell complexAndFraction.hs
#-}

module Main where

import Data.Complex
import Data.Ratio
import Text.Printf


{-#  복소수 출력하는 함수 #-}
printC :: (RealFloat a, Show a) => Complex a -> IO ()
printC a = print ((show (realPart a)) ++ " + " ++ (show (imagPart a)) ++ "j")

{-#  분수 출력하는 함수 #-}
printF :: (Integral a, Num a, Eq a, Show a) => Ratio a -> IO ()
printF a = print ((show(numerator a)) ++ "/" ++ (show (denominator a)))

{-#  (인용 부호 없이) 복소수 출력하는 함수 #-}
-- printfFraction :: (PrintfArg a, Show a, Eq a, Integral a) => Ratio a -> IO ()
printFraction :: (Show a, Integral a) => Ratio a -> IO ()
printFraction a = printf "%s/%s\n" (show (numerator a)) (show (denominator a))

{-#  (인용 부호 없이) 분수 출력하는 함수 #-}
-- printfComplex :: (Num a, RealFloat a, Show a, PrintfArg a) => Complex a -> IO ()
printComplex :: (Show a, RealFloat a) => Complex a -> IO ()
printComplex a = printf "%s + %sj\n" (show (realPart a)) (show (imagPart a))


main :: IO ()       
main = do
    -- 몫과 나머지 구하기
    print (div 5 3)
    print (mod 5 3)
    print (divMod 5 3)
    print (5 `div` 3)
    print (5 `mod` 3)
    print (5 `divMod` 3)

    -- 분수 생성과 계산
    print (5 % 3)
    print (5 % 3 * 2)
    print (5 % 3 + 2)
    print (5 % 3 + 2 % 3)

    -- 복소수 생성과  계산
    print (2 :+ 3)
    print (sqrt 2 :+ 3)
    print (sqrt (2 :+ 3))
    print (2 :+ 3 * 2)
    print ((2 :+ 3) - 2)
    print ((2 :+ 3) - (0:+2))
    print ((2 :+ 3) * (0:+2))
    print (conjugate (2 :+ 3))
    print (1 / 2 :+ 3)
    print (1 / (2 :+ 3))

    let x = 2 :+ 5
    printf "x = %s + %sj\n" (show (realPart x)) (show (imagPart x))

    -- 복소수 츨력하기 --
    printC x
    printC (conjugate x)
    printC (sin x)
    printC (asin (sin x))
    printC ((asin . sin) x)

    -- 분수 츨력하기
    let b = 2 % 3
    printF b
    printF (1/b)
    printF ((1/b)*2)

    -- (인용 부호 없이) 분수 츨력하기
    printFraction b
    printFraction (1/b)
    printFraction ((1/b)*(2%1))

    -- (인용 부호 없이) 복소수 츨력하기
    printComplex x
    printComplex (conjugate x)
    printComplex (sin x)
    printComplex (asin (sin x))
    printComplex ((asin . sin) x)
{-
------
Output:
------
1
2
(1,2)
1
2
(1,2)
5 % 3
10 % 3
11 % 3
7 % 3
2.0 :+ 3.0
1.4142135623730951 :+ 3.0
1.6741492280355401 :+ 0.8959774761298381
2.0 :+ 6.0
0.0 :+ 3.0
2.0 :+ 1.0
(-6.0) :+ 4.0
2.0 :+ (-3.0)
0.5 :+ 3.0
0.15384615384615385 :+ (-0.23076923076923078)
x = 2.0 + 5.0j
"2.0 + 5.0j"
"2.0 + -5.0j"
"67.47891523845588 + -30.879431343588244j"
"1.1415926535897933 + -5.0j"
"1.1415926535897933 + -5.0j"
"2/3"
"3/2"
"3/1"
2/3
3/2
3/1
2.0 + 5.0j
2.0 + -5.0j
67.47891523845588 + -30.879431343588244j
1.1415926535897933 + -5.0j
1.1415926535897933 + -5.0j
-}

 

 

 

Posted by Scripter
,