$30 off During Our Annual Pro Sale. View Details »

하스켈과 타입

하스켈과 타입

하스켈과 타입에 대해 설명합니다.

2015년 4월 4일 하스켈 프로그래밍 다섯번째 모임
http://haskell.kr/meetup/2015-04-04/

Hong Minhee (洪 民憙)

April 04, 2015
Tweet

More Decks by Hong Minhee (洪 民憙)

Other Decks in Programming

Transcript

  1. ೞझுҗ ఋੑ
    ഘ޹൞
    http://hongminhee.org/

    View Slide

  2. Data type
    • օܻ ॳ੉ח ߣ৉য: ੗ܐഋ
    • ୶ஏೡ ࣻ ੓٠, ੗ܐ(data)ܳ ࠙ܨ(type)ೞח Ѫ
    • Ӓۢ ࠙ܨೠ ੗ܐٜ਷ যڌѱ ࢤӟ Ѫੌө?

    View Slide

  3. Value
    • օܻ ॳ੉ח ߣ৉য: ч
    • য٥оী ੸ഃࢲ ա઺ী ੍൧ ࣻ ੓׮
    • ੉ଃীࢲ ੷ଃਵ۽ ੹೧઴ ࣻ ੓׮

    View Slide

  4. Value
    • օܻ ॳ੉ח ߣ৉য: ч
    • ݫݽܻա ٣झ௼ী ੷੢ೞҊ ա઺ী ৌযࠅ ࣻ ੓׮
    • ֎౟ਕ௼ܳ ా೧ ੹࣠ؼ ࣻ ੓׮

    View Slide

  5. Value
    • օܻ ॳ੉ח ߣ৉য: ч
    • ੉ܴ૑য૕ ࣻب ੓Ҋ, ੉ܴ੉ যڃ чਸ ڷೞח૑ب ঌ ࣻ ੓׮
    • ೣࣻ ੋ੗۽ ٜযт ࣻب ੓Ҋ, ೣࣻ۽ࠗఠ աৢ ࣻب ੓׮

    View Slide

  6. ч
    > let factorial n = product [1..n]
    > let x = 10
    > factorial x
    3628800

    View Slide

  7. ੉ܴ૑য૕ ࣻ ੓׮
    > let factorial n = product [1..n]
    > let x = 10
    > factorial x
    3628800

    View Slide

  8. ੉ܴ੄ ڷਸ ঌ ࣻ ੓׮
    > let factorial n = product [1..n]
    > let x = 10
    > factorial x
    3628800

    View Slide

  9. ೣࣻ ੋ੗۽ ٜযт ࣻ ੓׮
    > let factorial n = product [1..n]
    > let x = 10
    > factorial x
    3628800

    View Slide

  10. ೣࣻ۽ࠗఠ աৢ ࣻ ੓׮
    > let factorial n = product [1..n]
    > let x = 10
    > factorial x
    3628800

    View Slide

  11. ч੄ ࢿ૕
    • чٜ਷ ੷݃׮ ةౠೠ ࢿ૕੉ ੓׮
    • 1 ׮਺਷? 2. Ӓۢ 2 ׮਺਷? 3.
    • ['a', 'b', 'c']੄ ӡ੉ח? 3.
    • [] ׮਺਷? ݾ۾ೠప ‘׮਺’੉ۆ ަө?
    • 1੄ ӡ੉ח? ੿ࣻೠప ‘ӡ੉’ۆ ަө?

    View Slide

  12. ч੄ ࢿ૕
    > succ 1
    2
    > succ 2
    3
    > length ['a', 'b', 'c']
    3

    View Slide

  13. ч੄ ࢿ૕
    > succ ['a', 'b', 'c']
    :16:1:
    No instance for (Enum [Char]) arising from a
    use of ‘succ’
    In the expression: succ ['a', 'b', 'c']
    In an equation for ‘it’: it = succ ['a', 'b',
    'c']

    View Slide

  14. ч੄ ࢿ૕
    > length 1
    :17:8:
    No instance for (Num [a0]) arising from the
    literal ‘1’
    In the first argument of ‘length’, namely ‘1’
    In the expression: length 1
    In an equation for ‘it’: it = length 1

    View Slide

  15. ੗ܐഋ
    • чٜ਷ ੷݃׮ ةౠೠ ࢿ૕੉ ੓׮
    • Ӓ۞ա ࢿ૕੉ ൚ࢎೠ Ѫՙܻ ݽইࢲ ࠙ܨೡ ࣻ ੓׮
    • ‘׮਺’੉ ੓Ҋ, ف ч੉ ‘׮਺’ чٜ੉ ‘эਵݶ’ ف ч੉ ‘эҊ’,
    ‘؊ೡ ࣻ’ ੓Ҋ, ‘ࡓ ࣻ’ب ੓Ҋ… → ੿ࣻ ੗ܐഋ
    • 0ѐ ੉࢚੄ чਸ ‘ನೣ’ೡ ࣻ ੓Ҋ, ನೣػ чٜ ࢎ੉ী ‘ࣽࢲ’
    о ੓Ҋ… → ݾ۾ ੗ܐഋ

    View Slide

  16. ੗ܐഋ
    • ࠺तೠ ࢿ૕੄ чٜਸ ݽইفݶ ಞܻೞ׮
    • о۸, ؔࣅਸ ೡ ٸ ‘؊ೡ ࣻ ੓ח’ ࢿ૕੄ чٜ݅ ߉Ҋ र׮
    → ࣻ ੗ܐഋ݅ ߉ਵݶ ػ׮
    • о۸, іࣻܳ ҳೡ ٸ ‘чਸ ನೣೡ ࣻ ੓ח’ ࢿ૕੄ чٜ݅
    ߉Ҋ र׮ → ݾ۾ ੗ܐഋ݅ ߉ਵݶ ػ׮

    View Slide

  17. factorial਷ ੿ࣻ݅ ߉ח׮
    > factorial ['a', 'b', 'c']
    :26:1:
    No instance for (Num [Char]) arising from a
    use of ‘factorial’
    In the expression: factorial ['a', 'b', 'c']
    In an equation for ‘it’: it = factorial ['a',
    'b', 'c']

    View Slide

  18. ч੄ ੗ܐഋ
    (पઁח ੉ࠁ׮ ખ؊ ࠂ੟೤פ׮)
    > let factorial n =
    product ([1..n] :: [Integer]) :: Integer
    > let x = 10 :: Integer
    > factorial x :: Integer
    3628800 :: Integer

    View Slide

  19. ੗ܐഋ ঌইࠁӝ
    (पઁח ੉ࠁ׮ ખ؊ ࠂ੟೤פ׮)
    > :t x
    x :: Integer
    > :t factorial
    factorial :: Integer -> Integer

    View Slide

  20. ৈӝࢶ ׮ܖ૑ ঋח ׮਺ ઱ઁ
    • ؀ࣻ੸ ੗ܐഋ (algebraic data type)
    • Parametric polymorphism
    • ఋੑ௿ېझ (typeclass)
    • ⊥ (bottom)
    • ӝఋ ١١. ೞ૑݅ ށۄب ੌױ ೞझு ௏٬ द੘ оמ

    View Slide

  21. ࠗप೧ࢲ દ࣠೤פ׮
    bit.ly/haskell-kr-2015-04-04-type

    View Slide