ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Haskell] #5 Type (2)
    ๊ณต๋ถ€/ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ 2020. 1. 20. 21:45

    ์–ด์ œ ์ €ํฌ๋Š” Haskell์—์„œ์˜ Type์ด๋ž‘, ํ•จ์ˆ˜ Type ์„ ์–ธ๊ณผ, ์ œ๋„ค๋ฆญ ์ฒ˜๋Ÿผ ํ•จ์ˆ˜์˜  ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋‹ค์–‘ํ•œ ํƒ€์ž…์„ ํ—ˆ์šฉํ•˜๋Š” ํƒ€์ž… ๋ณ€์ˆ˜, ๊ทธ๋ฆฌ๊ณ  ์ผ๋ฐ˜์ ์ธ Type๋“ค์— ๋Œ€ํ•ด์„œ ๋‹ค๋ค˜์–ด์š”.

     

    2020/01/19 - [๊ณต๋ถ€/ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ] - [Haskell] #4 Type (1)

     

    ์˜ค๋Š˜์€ Type Class์™€ Haskell์—์„œ ๋งŽ์ด ์“ฐ๋Š” Type Class๋ฅผ ์ •๋ฆฌํ•ด๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค.

     

     

    1. GHCi ์—์„œ :t ๋กœ ํƒ€์ž… ์•Œ์•„๋ณด๊ธฐ
    2. ํ•จ์ˆ˜ Type์„ ์„ ์–ธํ•˜๊ธฐ
    3. ํƒ€์ž… ๋ณ€์ˆ˜. (ํ•˜์Šค์ผˆ ๊ณ„์˜ ์ œ๋„ค๋ฆญ ์‚ฌ์šฉ๋ฒ•?)
    4. ํ•˜์Šค์ผˆ์˜ ์ผ๋ฐ˜์ ์ธ ํƒ€์ž…๋“ค
    5. Type Class
    6. ํ•˜์Šค์ผˆ์—์„œ ๋งŽ์ด ์“ฐ๋Š” Type Class ๋“ค
    7. ๋งˆ๋ฌด๋ฆฌ

    5. Type Class

     

    ํƒ€์ž… ํด๋ž˜์Šค๋Š” ์–ด๋–ค ๋™์ž‘์„ ์ •์˜ํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค. 

     

    ๊ฐ์ฒด ์ง€ํ–ฅ์—์„œ ๋งํ•˜๋Š” Class์™€๋Š” ๋‹ค๋ฆ…๋‹ˆ๋‹ค.  (?? ์•„์ง์€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค.)

     

     

    1. Type Class๋Š” ๋™์ž‘๋งŒ ๋ช…์‹œํ•ฉ๋‹ˆ๋‹ค. ์†์„ฑ์ด ์—†์Šต๋‹ˆ๋‹ค.

     

    2. ๋‹ค์ค‘์˜ Type Class์˜ Instance๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ํƒ€์ž…์ด ์—ฌ๋Ÿฌ ๊ฐœ์˜ Type Class๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    ์˜ˆ๋ฅผ ๋“ค์–ด, ์ˆซ์ž๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํƒ€์ž… ์ค‘ ํ•˜๋‚˜์ธ Integer๋Š” == ์—ฐ์‚ฐ๋„ ๋˜๊ณ , <= ์—ฐ์‚ฐ๋„ ๋ฉ๋‹ˆ๋‹ค. ์ด ๋‘˜์€ ๊ฐ๊ฐ Eq, Ord ๋ผ๋Š” Type Class๋ฅผ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Integer๋Š” 2๊ฐœ์˜ Type Class๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” Instance์ž…๋‹ˆ๋‹ค.

     

    3. Type Class๋Š” ๋‹ค๋ฅธ Type Class์˜ Instance๊ฐ€ ๋˜๊ธฐ ์œ„ํ•œ ์„ ํ–‰ ์กฐ๊ฑด์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    ์˜ˆ๋ฅผ ๋“ค์–ด,  ๋น„๊ต๋ฅผ ์œ„ํ•œ Type Class์ธ Ord์˜ <=๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ==(Eq)์˜ ๊ตฌํ˜„์ด ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Ord๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Eq๋ฅผ ๋ฐ˜๋“œ์‹œ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

     

    (ํฌ๊ฒŒ ๋‹ค๋ฆ…๋‹ˆ๋‹ค๋งŒ ์•„์ง๊นŒ์ง€๋Š” ๊ฐ์ด ์ž˜ ์˜ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. Java์˜ ์ธํ„ฐํŽ˜์ด์Šค ๋ณด๋‹ค๋Š”, Swift์™€ ObjectiveC์˜ protocol ๊ฐœ๋…์— ๊ฐ€๊น์Šต๋‹ˆ๋‹ค.)

     

    ์–ด๋–ค ํ•จ์ˆ˜๊ฐ€ ์–ด๋–ค Type Class๋ฅผ ๋”ฐ๋ฅธ๋‹ค๋ฉด, ๊ทธ ํ•จ์ˆ˜๋Š” Type Class์—์„œ ์ง€์ •ํ•œ ๋™์ž‘์„ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

     

    Type Class์˜ ์˜ˆ๋กœ ==์˜ Type Signature๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    :t (==)  // ==, /=์™€ ๊ฐ™์€ ์ค‘์œ„ ํ•จ์ˆ˜๋ฅผ ์ „์œ„ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์“ฐ๊ฑฐ๋‚˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„˜๊ฒจ์ฃผ๋ ค๋ฉด  ()๋กœ ๊ฐ์‹ธ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    (==) :: Eq a => a -> a -> Bool

     

    ์œ„ ์‹œ๊ทธ๋‹ˆ์ฒ˜๋ฅผ ๋ณด๋ฉด == ์—ฐ์‚ฐ์ž๋„ ํ•จ์ˆ˜์ด๊ณ , ๊ฐ™์€ ํƒ€์ž… (ํƒ€์ž… ๋ณ€์ˆ˜ a)๋ฅผ 2๊ฐœ๋ฅผ ๋ฐ›์•„, Bool์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

    ๊ทธ๋Ÿฐ๋ฐ ๊ทธ ์•ž์— Eq a => ๋ผ๋Š” ์ƒˆ๋กœ์šด ๋ฌธ์ž๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    =>

    =>๋Š” class Constraint๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์—ฐ์‚ฐ์ž์ž…๋‹ˆ๋‹ค. ์ด ์—ฐ์‚ฐ์ž ์•ž์— ์˜ค๋Š” ๊ฒƒ๋“ค์„ class Constraint๋ผ๊ณ , ํ•˜๋ฉฐ, ์–ด๋–ค ํ•จ์ˆ˜์— ๋“ค์–ด๊ฐ€๋Š” ํƒ€์ž…์„ ์ œํ•œํ•ฉ๋‹ˆ๋‹ค. 

     

    ํ•จ์ˆ˜๋ช… :: class Constraint => ํ•จ์ˆ˜ ํƒ€์ž… ์„ ์–ธ

    (==) :: Eq a => a -> a -> Bool

    ์œ„์˜ ==์˜ ๊ฒฝ์šฐ, Eq ์ด๋ผ๋Š” Type Class๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋Š” a ๋ผ๋Š” Type์„ 2๊ฐœ ๋ฐ›์•„ Bool์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค ๋ผ๋Š” ์˜๋ฏธ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

     

    Type Class๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋Š” Type์„ Type Class์˜ Instance๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

     

    ==๊ฐ€ ์–ด๋””์—์„œ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•œ์ง€๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์•˜์„ ๋•Œ, ์•ž์„œ ์ €ํฌ๊ฐ€ ์“ฐ๋˜ 3, 'c', "hi" ๋“ฑ๋“ฑ์˜ Type๋“ค๋„ Eq ํƒ€์ž… ํด๋ž˜์Šค์˜ Instance ์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    6. Haskell์—์„œ์˜ ์ผ๋ฐ˜์ ์ธ Type Class

     

    1. Eq

    Eq๋Š” ํƒ€์ž…์ด ์„œ๋กœ ๊ฐ™์€์ง€๋ฅผ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค. (Equatable)

     

    ์ด Type Class์— ์†ํ•˜๋Š” Type์ด ๊ตฌํ˜„ ํ•ด์•ผํ•˜๋Š” ํ•จ์ˆ˜๋Š” ==, /= ์ž…๋‹ˆ๋‹ค.

     

    ๋งŒ์ผ ์–ด๋–ค ํ•จ์ˆ˜์˜ ํƒ€์ž…์— Eq ์˜ Class Constraint๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค๋ฉด, ๊ทธ ํ•จ์ˆ˜๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ==, /=๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋Š” ์˜๋ฏธ ์ž…๋‹ˆ๋‹ค.

    5 == 5 //True
    'c' /= 'b' //True
    'c' /= 'c' //False

    Eq ํƒ€์ž… ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ง€๋ ค๋ฉด, ==, /=๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค๊ณ  ์ •์˜๊ฐ€ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. 

     

    Haskell์˜ ๋ ˆํผ๋Ÿฐ์Šค์—์„œ๋„ 5 ์˜ ํƒ€์ž…์ธ Num์ด Eq๋ฅผ Class Constraint๋กœ ๊ฐ–๊ณ  ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    http://zvon.org/other/haskell/Outputprelude/Num_c.html

     

    ํ•จ์ˆ˜์™€ ์ž…์ถœ๋ ฅ ํƒ€์ž…๋“ค์„ ์ œ์™ธํ•œ, ๋ชจ๋“  ํ•˜์Šค์ผˆ ํ‘œ์ค€ ํƒ€์ž…์ด Eq๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.

     

     

     

    2. Ord

    • Ord๋Š” ์„œ๋กœ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋Š” ์—ฐ์‚ฐ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. (Order)
    • <, <=, >, >=, compare ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค
    • <=, >= ์—์„œ ๋ณด๋‹ค์‹œํ”ผ, Eq์„ Class Constraints๋กœ ๊ฐ€์ง‘๋‹ˆ๋‹ค. Eq์ด ํ•ญ๋“ฑ ๋น„๊ต๋ฅผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด์ฃ .
    :t (<)
    (<) :: Ord a => a -> a -> Bool

    http://zvon.org/other/haskell/Outputprelude/index.html

    http://zvon.org/other/haskell/Outputprelude/index.html

    ์œ„์—์„œ ํƒ€์ž… ํ™•์ธ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

     

    compare 

    compare๋Š” ์ธ์ž๋กœ ๊ฐ™์€ ํƒ€์ž… 2๊ฐœ๋ฅผ ๋ฐ›์•„ Ordering (GT ๋‚ด๋ฆผ์ฐจ์ˆœ, LT ์˜ค๋ฆ„์ฐจ์ˆœ, EQ ๊ฐ™์Œ)๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

     

    compare 1 3
    => LT //์˜ค๋ฆ„์ฐจ์ˆœ
    compare 3 1
    => GT //๋‚ด๋ฆผ์ฐจ์ˆœ
    compare 2 2
    => EQ //๊ฐ™์Œ
    compare 2 (-2) //์Œ์ˆ˜๋Š” ๊ผญ ()๋กœ ๊ฐ์‹ธ์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค
    => GT

    //์ค‘์œ„ ์—ฐ์‚ฐ์ž์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ``(backqoute)๋กœ ๊ฐ์‹ธ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    3 `compare` 4
    => LT

     

    ๋น„๊ต ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  Type์ด ์ด Ord๋ฅผ Class Constraints๋กœ ๊ฐ–์Šต๋‹ˆ๋‹ค. 

     

    3. show 

    show ํƒ€์ž… ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” Instance๋“ค์€ ๋ฌธ์ž์—ด์ฒ˜๋Ÿผ ํ‘œ์‹œ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

    show ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด, ์ด ํƒ€์ž…์„ ๊ตฌํ˜„ํ•˜๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    show 3
    => "3"

    show 'c'
    => "'c'"

    show "hello"
    => "\"hello\""

    http://zvon.org/other/haskell/Outputprelude/index.html

     

    ํ•จ์ˆ˜ ํƒ€์ž… ์ œ์™ธ, ์ผ๋ฐ˜์ ์ธ ํƒ€์ž…๋“ค์€ ๋ชจ๋‘ show๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

     

    4. read 

    • readํƒ€์ž…์€ show์˜ ๋ฐ˜๋Œ€์ž…๋‹ˆ๋‹ค.
    • ๋ฌธ์ž์—ด์„ ์ฝ์–ด๋“ค์—ฌ, Read ์ธ์Šคํ„ด์Šค๋กœ ๋ณ€ํ™˜ํ•ด์ค๋‹ˆ๋‹ค. 
    • 4, 'c' ๋“ฑ๋“ฑ์˜ ๊ธฐ๋ณธ์ ์ธ ํด๋ž˜์Šค๋“ค์€ ๋ชจ๋‘ ์ด ํƒ€์ž… ํด๋ž˜์Šค๋ฅผ class constraint๋กœ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค

     


    read "5" // ๋ฌธ์ž์—ด๋กœ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์—, ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์ด ๊ฐ’์ด ์ •๋ง ๋ฌธ์ž์—ด์ธ์ง€, ์•„๋‹ˆ๋ฉด Int์˜ 5์ธ์ง€๋ฅผ ์•Œ ์ˆ˜ ์—†์–ด ์•„๋ž˜์ฒ˜๋Ÿผ ์—๋Ÿฌ๋ฅผ ๋ฑ‰์Šต๋‹ˆ๋‹ค. ์ •๋ณด๊ฐ€ ๋ถ€์กฑํ•ด์„œ ํƒ€์ž… ์ถ”๋ก ์— ์‹คํŒจํ•œ ๊ฒƒ์ด์ฃ .
    *** Exception: Prelude.read: no parse read

    read "5" - 2  // ์ด์ฒ˜๋Ÿผ ๋‹ค๋ฅธ ํ‘œํ˜„์‹์ด ๋“ค์–ด๊ฐ€๋ฉด ํ•˜์Šค์ผˆ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํƒ€์ž…์„ ์ถ”์ •ํ•  ์ˆ˜ ์žˆ์–ด ์˜ค๋ฅ˜๋ฅผ ๋‚ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์บ์ŠคํŒ…์ด ํ•„์š” ์—†๋Š” ๊ฒƒ์ด ์žฅ์ ์ด๋„ค์š”.
    => 3 


    [read "True", False, read"False"]
    => True, False, False


    read "5" :: Int // :: (Type Annotation) ์ด๋ ‡๊ฒŒ  ๋‹จ๋…์œผ๋กœ ์“ฐ์ผ ๋•Œ๋Š”  :: ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด ํƒ€์ž…์„ ๋ช…์‹œํ•ด์ฃผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋‚˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    => 5

    read "5" :: Float
    => 5.0
     

     

    5. Enum

    • ์—ฐ์†์ ์œผ๋กœ ์ˆœ์„œ๊ฐ€ ์žˆ๋Š” ํƒ€์ž…์ž…๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ํ•ญ๋ชฉ๋“ค์ด ์ด Type Class๋ฅผ class constraint๋กœ ๊ฐ–์Šต๋‹ˆ๋‹ค.
    • succ(๋‹ค์Œ), pred (์ด์ „) ํ•จ์ˆ˜๊ฐ€ ์—ฌ๊ธฐ์— ์†ํ•ฉ๋‹ˆ๋‹ค.
    • Int, Char, () (๋นˆ ํŠœํ”Œ) ์ด ์ด ํด๋ž˜์Šค์— ์†ํ•ฉ๋‹ˆ๋‹ค.
    • ์ด Type Class๋Š” Range๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด ์ค๋‹ˆ๋‹ค.
    succ 'a'
    => 'b'

    pred 'a'
    => '`'

    pred 'b'
    => 'a'

    ['a' .. 'c'] 
    => a, b, c

    [1 .. 3]
    => 1, 2, 3

     

    6. Bounded

    • ์ด Type Class๋ฅผ ๊ตฌํ˜„ํ•œ ์ธ์Šคํ„ด์Šค๋“ค์€ minBound์™€ maxBound๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค. 
    • ์˜ˆ๋กœ  Int, Bool, Tuple(์›์†Œ๊ฐ€ ๋ชจ๋‘ Bounded๋ฅผ ๊ตฌํ˜„ํ•  ๊ฒฝ์šฐ) ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. 
    • minBound, maxBound์˜ ํƒ€์ž…์€ Bounded a => a์ž…๋‹ˆ๋‹ค. ๋Œ๋ ค์ฃผ๋Š” ๊ฐ’์€ ์ƒ์ˆ˜์ธ๋ฐ์š”, ์ด ์ƒ์ˆ˜ a๋Š” ์ด Bounded๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์–ด๋–ค ํƒ€์ž…๋„ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๋‹คํ˜• ์ƒ์ˆ˜๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. (Polymorphic constant)

     


    :t minBound
    => minBound :: Bounded a => a


    minBound :: Int
    => -9223372036854775808

    minBound :: Float  //Float ํƒ€์ž…์€ ์ด๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    <interactive>:5:1: error:
    • No instance for (Bounded Float) arising from a use of ‘minBound’ •
    In the expression: minBound :: Float In an equation for ‘it’: it = minBound :: Float

    minBound :: Bool
    => False

     

    7. Num 

    • ์ˆซ์ž๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํด๋ž˜์Šค
    • Show, Eq์„ class constraints๋กœ ๊ฐ–์Šต๋‹ˆ๋‹ค. 
    • ์ด๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” Instance ๋กœ๋Š” Integral, Floating์ด ์žˆ์Šต๋‹ˆ๋‹ค.
    :t 3
    3 :: Num p => p

    //Num ํƒ€์ž… ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ฌด์—‡์ด๋“  ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋˜ํ•œ ์œ„์™€ ๊ฐ™์€ ๋‹คํ˜•  ์ƒ์ˆ˜ ์ด๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . Float๋„, Int๋„, Integer๋กœ๋„ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    // :: ์œผ๋กœ ํƒ€์ž…์„ ๋ฐ”๊ฟ”์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

    3 :: Integer
    => 3

    3 :: Float
    => 3.0

    3 :: Double
    => 3.0

     

    ๋‹ค๋งŒ, * , /,-,+ ๋“ฑ์˜ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๊ฐ™์€ ํƒ€์ž…์ด์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ผ๋ถ€๋Ÿฌ ์•„๋ž˜์™€ ๊ฐ™์ด Num์˜ ๋‹ค๋ฅธ ์ธ์Šคํ„ด์Šค๋กœ ํ˜•๋ณ€ํ™˜ ํ•ด์„œ ๊ณ„์‚ฐํ•˜๋ ค๊ณ  ํ•˜๋ฉด ๋™์ž‘ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

     


    :t (*)
    (*) :: Num a => a -> a -> a

    (5 :: Int ) * (10::Integer)  //๊ฐ™์€ Num ์ผ์ง€๋ผ๋„ ํƒ€์ž…์ด ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๊ณ„์‚ฐ์ด ์•ˆ๋ฉ๋‹ˆ๋‹ค.

    => <interactive>:27:16: error:

     

    Integral

    ์ •์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” Type Class ์ž…๋‹ˆ๋‹ค. Int, Integer๊ฐ€ ์—ฌ๊ธฐ์— ์†ํ•ฉ๋‹ˆ๋‹ค.

     

    fromIntegral์ด๋ผ๋Š” ํ•จ์ˆ˜๋กœ, Integral์„ Num์œผ๋กœ ๋ณ€ํ™˜์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•ด์„œ, Float ๋“ฑ ๋‹ค๋ฅธ  Num ๋‚ด์˜ ํƒ€์ž…๋“ค๊ณผ ๊ณ„์‚ฐ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


    fromIntegral (4::Int) //์—ฌ๊ธฐ์„œ 4๋Š” Int ์ด๊ณ , ๋‹คํ˜• ์ƒ์ˆ˜๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.
    => 4 //์—ฌ๊ธฐ์„œ๋Š” Num a 

    //์ด ๊ฒฝ์šฐ, Float๊ณผ ๋ง์…ˆ์ด ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.


    :t fromIntegral
    fromIntegral :: (Integral a, Num b) => a -> b //Integral a๋ฅผ ๋ฐ›์•„, Num์ธ b๋ฅผ ๋Œ๋ ค์ค๋‹ˆ๋‹ค.

    //fromIntegral์„ ์‚ฌ์šฉํ•˜๋ฉด 

     

    Floating

    ๋ถ€๋™์†Œ์ˆ˜์ ์„ ๋‚˜ํƒ€๋‚ด๋Š” Type Class์ž…๋‹ˆ๋‹ค. Float, Double์ด ์—ฌ๊ธฐ์— ์†ํ•ฉ๋‹ˆ๋‹ค. 

     

    7. ๋งˆ๋ฌด๋ฆฌ

     

    Type Class๋Š” ๊ฐ์ฒด ์ง€ํ–ฅ์—์„œ์˜ ํด๋ž˜์Šค์™€๋Š” ๋‹ฌ๋ฆฌ, ๊ทธ์ € ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ํ•จ์ˆ˜์˜ ํƒ€์ž…๋งŒ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. 

     

    1. ํ•˜๋‚˜์˜ ํƒ€์ž…์ด ์—ฌ๋Ÿฌ Type Class์˜ Instance(๊ตฌํ˜„์ฒด)๊ฐ€ ๋  ์ˆ˜ ์žˆ๊ณ  

    2. ํ•˜๋‚˜์˜ Type Class๋Š” ์—ฌ๋Ÿฌ ํƒ€์ž…์„ ๋™์‹œ์— Instance๋กœ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    3. ๊ทธ๋ฆฌ๊ณ  ํ•œ Type Class๊ฐ€ ๋‹ค๋ฅธ Type Class๋ฅผ ๊ฐ€์ ธ๊ฐ€๊ธฐ ์œ„ํ•œ ์ „์ œ ์กฐ๊ฑด์ด ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

     

    ๋Œ€ํ‘œ์ ์œผ๋กœ Num ํด๋ž˜์Šค๋Š” Show ์™€ Eq๋ฅผ ๋™์‹œ์— ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๊ณ , ๋˜ Eq ์€ Num ๋ฟ๋งŒ์ด ์•„๋‹ˆ๋ผ Char๋„ Instance๋กœ  ๊ฐ–์Šต๋‹ˆ๋‹ค. 

    ๋˜ํ•œ Ord์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋˜๋ ค๋ฉด ๊ทธ ์ „์— Eq์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋˜์–ด์•ผ ํ•˜์ฃ . (์ „์ œ ์กฐ๊ฑด์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค)

     

    ์ด๋ ‡๊ฒŒ 2์žฅ Type ์˜ ์ •๋ฆฌ๊ฐ€ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค...

     

    + ํ›„๋‹ด...

     

    ์œผ์•„์•„... ํ™•์‹คํžˆ Type Class๋Š” ์ผ๋ฐ˜์ ์ธ ๊ฐ์ฒด ์ง€ํ–ฅ์˜ ํด๋ž˜์Šค๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ํ•จ์ˆ˜์˜ ์ •์˜๋งŒ ํ•ด๋‘๊ณ , ๊ทธ๊ฑธ ๊ฐ๊ฐ์˜ ํƒ€์ž…์ด ๊ฐ€์ ธ๊ฐ€์„œ ์•Œ์•„์„œ ๊ตฌํ˜„ํ•˜๋„๋ก ๋‚ด๋ฒ„๋ ค๋‘ก๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  TypeClass๋Š” ์ƒ์†์˜ ๊ฐœ๋… ๋ณด๋‹ค๋Š” ์ „์ œ ์กฐ๊ฑด๊ฐ™์€ ์ˆ˜ํ•™์ด๋‚˜ ๋…ผ๋ฆฌํ•™์—์„œ์˜ '์กฐ๊ฑด'์— ๊ฐ€๊นŒ์šด ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

     

    ์•„, ์ฑ…์—์„œ ๋ณด๋Š” ๊ฑด ์กฐ๊ธˆ ํ—ท๊ฐˆ๋ ธ๋Š”๋ฐ, ์‹ค์ œ API ๋ฌธ์„œ์—์„œ ํ•ด๋‹น ํด๋ž˜์Šค์˜  ์ฐพ์•„์„œ ๋ณด๋‹ˆ ์กฐ๊ธˆ ๋” ์ดํ•ด๊ฐ€ ์ž˜ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 

    (์•„์ง ๋ฐฐ์šด ์ ์ด ์—†๋Š” ๋ฌธ๋ฒ•๋“ค์ด ๋‚˜์˜ค์ง€๋งŒ...)

     

    ์ด ๋‹ค์Œ์€ ๋“œ๋””์–ด ์žฌ๊ท€ ๋“ฑ๋“ฑ์„ ์ด์šฉํ•ด ๋ฐ˜๋ณต๋ฌธ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” 3์žฅ์ž…๋‹ˆ๋‹ค!! ์ด์ œ 3์žฅ์„ ๋ณด๋ฉด HackerRank์˜ 2๋ฒˆ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ์–ด์š”... ๊ฐ๊ฒฉ... ์–ด์„œ 3์žฅ์„ ๋นจ๋ฆฌ ์ •๋ฆฌ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด ์ข‹๊ฒ ๋„ค์š”. ๋‹ค์Œ ์ฃผ์—๋Š” 3์žฅ์œผ๋กœ ๋ต™๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ ๋–„ HackRank ๋ฌธ์ œ๋„ ํ•œ ๋ฒˆ ํ’€์–ด๋ณผ๊ฒŒ์š”!

     

    ์ฆ๊ฑฐ์šด ๋ฐค ๋˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค :)

    '๊ณต๋ถ€ > ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

    [Haskell] 5 - 3. $ (function application)  (0) 2020.02.16
    [Haskell] #6 syntax-in-functions (1)  (0) 2020.01.27
    [Haskell] #4 Type (1)  (0) 2020.01.19
    [Haskell] #3 Tuple  (0) 2020.01.12
    [Haskell] #2 List(2) Range, list comprehension  (0) 2020.01.12

    ๋Œ“๊ธ€

Designed by Tistory. @Bola Kim