ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Haskell] #6 syntax-in-functions (1)
    ๊ณต๋ถ€/ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ 2020. 1. 27. 21:43

    ์•ˆ๋…•ํ•˜์„ธ์š”. Lemon Kim ์ž…๋‹ˆ๋‹ค ๐Ÿ‹

     

    ์˜ค๋Š˜์€ ๋“œ๋””์–ด!! ํ•จ์ˆ˜์—์„œ pattern matching, guard์™€ where, let, case๋ฌธ์„ ํ†ตํ•ด์„œ ๋ฐ˜๋ณต์„ ํ•˜๊ฑฐ๋‚˜, if-else๋ฌธ์„ ๋Œ€์น˜ํ•ด์„œ ํ•จ์ˆ˜๋ฅผ ์งง๊ฒŒ ๋งŒ๋“œ๋Š” ๋ฒ•์„ ๋ฐฐ์šธ ์ˆ˜ ์žˆ์—ˆ์–ด์š”. ์ด ๋‹ค์Œ ์žฅ์ธ ์žฌ๊ท€๋ฅผ ๋ฐฐ์šฐ๊ณ  ๋‚˜๋ฉด, ๊ทธ๋ž˜๋„ HackerRank์˜ ๊ธฐ๋ณธ์ ์ธ ๋ฌธ์ œ๋“ค์„ ํ‘ธ๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ด ์งˆ ๊ฑฐ์—์š”.

     

    ์ด์ „ ํŽธ

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

     

    ์˜์–ด ์›๋ฌธ์€ ์—ฌ๊ธฐ http://learnyouahaskell.com/syntax-in-functions

     

     

    ๋ชฉ์ฐจ

     

    1. pattern matching

    1. Non-Exaustive์™€ ์ˆœ์„œ
    2. Factorial ๊ตฌํ˜„์œผ๋กœ ์•Œ์•„๋ณด๋Š” ์žฌ๊ท€
    3. ๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ๊ณผ ํŒจํ„ด ๋งค์นญ

     

    2. guard, where

    3. let

    4. case

     

    ์˜ค๋Š˜ ๋‹ค๋ฃฐ ๋‚ด์šฉ์€ 1๋ฒˆ pattern matching ์ด์—์š”! (์™œ ๋‚˜๋ˆ„์—ˆ๋ƒ ํ•˜๋ฉด... ์˜ค๋Š˜๋„ ์ •๋ฆฌํ•  ๋‚ด์šฉ์ด ๋„ˆ๋ฌด ๋งŽ์•„์ ธ์„œ...)

    1. pattern matching

    ํŒจํ„ด ๋งค์นญ์„ ์‚ฌ์šฉํ•ด์„œ, ํŠน์ •ํ•œ ์ƒํ™ฉ์„ ๋ฏธ๋ฆฌ ์ •์˜ํ•ด์„œ ๋จผ์ € ์•ž์— ์ ์–ด๋‘๊ณ , ๋‚˜๋จธ์ง€๋Š” x ๋“ฑ์˜ ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    ์˜ˆ๋ฅผ ๋“ค์–ด์„œ, ์ฑ…์—์„œ ๋‚˜์˜จ ์˜ˆ์ œ์ฒ˜๋Ÿผ ์ž…๋ ฅ์ด 7์ผ ๋•Œ๋Š” "lucky seven"์„, ๋‚˜๋จธ์ง€๋Š” "Oh, you're out of luck" ์„ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๊ฐ„๋‹จํ•œ ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

     

    lucky :: (Integral a) => a -> String  //ํ•จ์ˆ˜ ์„ ์–ธ

    lucky 7 = "LUCKY NUMBER SEVEN!" 
    //์—ฌ๊ธฐ๋ถ€ํ„ฐ ํ•จ์ˆ˜ body ์ž…๋‹ˆ๋‹ค. 7์— ๋Œ€ํ•œ ํŠน์ • ์ƒํ™ฉ์„ ๋ณด์—ฌ์ฃผ๋„ค์š”!

    lucky x = "Sorry, you're out of luck, pal!"
    // ๋‚˜๋จธ์ง€๋Š” ๋ณ€์ˆ˜ x๊ฐ€ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค. 

     

     

    1. Non-Exaustive์™€ ์ˆœ์„œ

     

    ๋ฒ”์œ„๊ฐ€ ์กด์žฌํ•˜๋Š” Char ๋“ฑ์˜ ๊ฒฝ์šฐ์—,  ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๋Œ€์‘ํ•ด ์ฃผ์ง€ ์•Š์€ ๊ฒฝ์šฐ, ๋Œ€์‘ํ•˜์ง€ ์•Š์€ ์ผ€์ด์Šค์˜ ์ž…๋ ฅ์ด ๋“ค์–ด์˜ค๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

     

    ์•„๋ž˜ ์˜ˆ๋ฅผ ๋ณด์ฃ !


    sayHi :: Char -> String
    sayHi 'a' = "Hello, " ++ "Ain"
    sayHi 'b' = "Hello, " ++ "Benjamin"
    sayHi 'c' = "Hello, " ++ "Carmen"

    sayHi 'a'
    =>Hello, Ain

    sayHi 'l' //์—†๋Š” ์ผ€์ด์Šค์ฃ ? 

    *** Exception: main.hs:(7,1)-(9,33): Non-exhaustive patterns in function sayHi

    exhaustive๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ์˜๋ฏธํ•œ๋‹ค๊ณ  ํ•ด์š”. (๋งํฌ๋Š” ๋ฏธ๋ฆฌ์—„ ์›น์Šคํ„ฐ ์‚ฌ์ „ ์ •์˜๋กœ)

     

    ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด, ์•„๋ž˜ ์ฝ”๋“œ์— x ๋“ฑ์˜ ๋ณ€์ˆ˜๋ฅผ ๋„ฃ์–ด์„œ ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•ด์ฃผ๋ฉด ๋˜๊ฒ ์ฃ ?

     


    sayHi :: Char -> String

    sayHi 'a' = "Hello, " ++ "Ain"

    sayHi 'b' = "Hello, " ++ "Benjamin"

    sayHi 'c' = "Hello, " ++ "Carmen" 

    sayHi x = "Hello, strange. what's your name?" //๋ชจ๋“  ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ์–ด์š”!


    sayHi 'a'

    => Hello, Ain


    sayHi 'l' //์—†๋Š” ์ผ€์ด์Šค์ฃ ? 

    => Hello, strange. what's your name? //์ด์ œ ์ž˜ ๋‚˜์˜ค๋„ค์š”

     

    ๊ทธ๋ฆฌ๊ณ  ๋‘ ๋ฒˆ์งธ๋กœ, ํŒจํ„ด ๋งค์นญ์€ ์œ„์—์„œ ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์ผ์–ด๋‚ฉ๋‹ˆ๋‹ค. ์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•ด์š”.

     


    sayHi :: (Integral a) => a -> String
    sayHi x = "hi, stranger!"
    sayHi 1 = "hi, 1"

    sayHi 1
    => hi, stranger!

     

    ์›๋ž˜๋Š” "hi, 1"์ด ๋‚˜์™€์•ผ ํ•˜๊ฒ ์ง€๋งŒ, "hi, stranger"๊ฐ€ ๋Œ€์‹  ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. 

     

    ํŒจํ„ด๋งค์นญ์ด ์ˆœ์„œ๋Œ€๋กœ ์ผ์–ด๋‚˜์„œ ์ƒ๊ธฐ๋Š” ํ˜„์ƒ์ž…๋‹ˆ๋‹ค. 

    ์œ„์ฒ˜๋Ÿผ x๊ฐ€ ๋ชจ๋“  ๊ฒƒ์„ ๋Œ€์‹ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ, ์‹คํ–‰์€ ๋˜์ง€๋งŒ ์•„๋ž˜ ๊ฐ™์€ warning์„ ์ถœ๋ ฅํ•˜๋„ค์š”.

     


    main.hs:3:1: warning: [-Woverlapping-patterns] Pattern match is redundant In an equation for ‘sayHi’: sayHi 1 = ... | 3 | sayHi 1 = "hi, 1" | ^^^^^^^^^^^^^^^^^ 

     

    2. Factorial ๊ตฌํ˜„์œผ๋กœ ์•Œ์•„๋ณด๋Š” ์žฌ๊ท€

     

    ํŒจํ„ด ๋งค์นญ์œผ๋กœ ์žฌ๊ท€๋ฅผ ๊ตฌํ˜„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

     

    Factorial์˜ ์ •์˜๋ฅผ ์ž ์‹œ ์ƒ๊ฐํ•ด๋ณผ๊นŒ์š”?

     

    factorial (0) = 1
    factorial (n) = n * factorial(n - 1) 

     

    ์œ„์˜ ๋ฌธ์ž๋ฅผ ํŒจํ„ด ๋งค์นญ์œผ๋กœ ๊ทธ๋Œ€๋กœ ์˜ฎ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    myFactorial :: (Integral a) => a -> a
    myFactorial 0 = 1
    myFactorial n = n * myFactorial (n-1) //์ด๋ฅผ ํ†ตํ•ด์„œ factorial์„ n ๋ฒˆ ๋ฐ˜๋ณตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


    myFactorial 3 => 6
    myFactorial 0 => 1

     

    ์ด๋ฅผ ์‘์šฉํ•ด, ๋ฐ˜๋ณต๋ฌธ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๊ฒ ๋„น์š”.

     

     

    3. ๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ๊ณผ ํŒจํ„ด ๋งค์นญ

    ๋‹น์—ฐํžˆ ๊ธฐ๋ณธ ํƒ€์ž…์ธ ํŠœํ”Œ, ๋ฆฌ์ŠคํŠธ์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ œ๋Š” ์œ„์˜ ๋งํฌ์˜ ๊ต์žฌ์—์„œ ๊ฐ–๊ณ  ์™”์Šต๋‹ˆ๋‹ค.

     

    ํŠœํ”Œ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ

    addVectors :: (Num a) => (a, a) -> (a, a) -> (a, a)  
    addVectors a b = (fst a + fst b, snd a + snd b)  

    //์œ„์˜ ํ•จ์ˆ˜๋ฅผ ์•„๋ž˜์ฒ˜๋Ÿผ ํŒจํ„ด ๋งค์นญ์„ ์‚ฌ์šฉํ•ด ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


    addVectors :: (Num a) => (a, a) -> (a, a) -> (a, a)  
    addVectors (x1, y1) (x2, y2) = (x1 + x2, y1 + y2)   

     

    ๋ฆฌ์ŠคํŠธ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ

    head' :: [a] -> a  
    head' [] = error "Can't call head on an empty list, dummy!"  //๋นˆ ๋ฆฌ์ŠคํŠธ์˜ ๊ฒฝ์šฐ๋ฅผ ํŒจํ„ด ๋งค์นญ์„ ํ™œ์šฉํ•ด ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
    head' (x:_) = x   //๋นˆ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์•„๋‹Œ ๋‚˜๋จธ์ง€ ๊ฒฝ์šฐ.

    ๋ฆฌ์ŠคํŠธ์—์„œ ํŒจํ„ด ๋งค์นญ์„ ์‚ฌ์šฉํ•  ๋•Œ, : ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ, x:y:z:[] ๋“ฑ์˜ ํ˜•ํƒœ๋กœ, ๋“ค์–ด์˜ค๋Š” ๋ฆฌ์ŠคํŠธ์˜ ๊ฐฏ์ˆ˜๋ฅผ ํŒจํ„ด ๋งค์นญ์œผ๋กœ ์ œํ•œํ•˜๊ฑฐ๋‚˜ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

     

    ์ฑ…์—์„œ๋Š” ์ด ํ™œ์šฉ์„ ํŠนํžˆ ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ ํ•  ๋•Œ, ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•˜๋Š”๋ฐ์š”, ์•„๋งˆ ๋‹ค์Œ ์žฅ์— ๋‚˜์˜ค์ง€ ์•Š์„๊นŒ ์‹ถ๋„ค์š”!

    tell :: (Show a) => [a] -> String  
    tell [] = "The list is empty"  
    tell (x:[]) = "The list has one element: " ++ show x  
    tell (x:y: []) = "The list has two elements: " ++ show x ++ " and " ++ show y  
    tell (x:y:_) = "This list is long. The first two elements are: " ++ show x ++ " and " ++ show y  

     

    ์˜ค๋Š˜์€ ์—ฌ๊ธฐ๊นŒ์ง€์—์š”! ๋‹ค์Œ ์‹œ๊ฐ„์—๋Š” guard์™€ where๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฒ•์„ ์ •๋ฆฌํ•ด๋ณด๋„๋ก ํ• ๊ฒŒ์š”.

     

    ์˜ค๋Š˜ ๊ณ ์ƒ ๋งŽ์œผ์…จ์–ด์š”. ์ข‹์€ ํ•˜๋ฃจ ๋˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค :)

     

     

     

     

    + ๋ณด๋„ˆ์Šค : ํŒŒ์ผ์„ ์ง์ ‘ ์ปดํŒŒ์ผํ•ด์„œ main ํ•จ์ˆ˜์—์„œ ์ถœ๋ ฅ ํ•  ๋•Œ ์ฃผ์˜ํ•  ์ . putStrLn์„ ์“ฐ๋ฉด show ๋“ฑ์œผ๋กœ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ์–ด์ฃผ์ž.

     

    ๊ณต๋ถ€๋„ ํ•  ๊ฒธ main ํŒŒ์ผ๋กœ ๋งŒ๋“ค์–ด์„œ ์ปดํŒŒ์ผ ํ•˜๋‹ˆ๊นŒ ๊ณ„์† ์•ˆ๋˜์„œ... ์—„์ฒญ ๋‹ต๋‹ตํ•ด ํ•˜๋‹ค ํ•ด๊ฒฐํ•ด์„œ ๋‚จ๊น๋‹ˆ๋‹ค.

     

    ๋ฌธ์ œ๊ฐ€ ๋œ main ํŒŒ์ผ


    myFactorial :: (Integral a) => a -> a
    myFactorial 0 = 1
    myFactorial n = n * myFactorial (n-1)

    main = do
          putStrLn (myFactorial 3)

    ์—๋Ÿฌ ๋ฉ”์‹œ์ง€

    main.hs:27:13: error: • No instance for (Integral String) arising from a use of ‘myFactorial’ • In the first argument of ‘putStrLn’, namely ‘(myFactorial 3)’

     

    ๊ณ„์† ์ด๋ ‡๊ฒŒ ์•ˆ ๋˜๊ธธ๋ž˜... ๋„๋Œ€์ฒด ๋ญ๊ฐ€ ๋ฌธ์ œ์ง€?!? ํ•˜๋‹ค๊ฐ€ ๋‚˜๋จธ์ง€ ์˜ˆ์ œ์„œ putStrLn์€ ๋ชจ๋‘ String์„ ์ธ์ž๋กœ ๋ฐ›๊ณ  ์žˆ์œผ๋‹ˆ... ์„ค๋งˆ...ํ•˜๊ณ  

    show ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ๋”๋‹ˆ... ํ•ด๊ฒฐ...

     

    ํ•ด๊ฒฐํ•œ main ํŒŒ์ผ

    //์œ„์™€ ๋‚ด์šฉ์€ ๊ฐ™์•„์š”... just show๋งŒ ์ถ”๊ฐ€...
    putStrLn (show(myFactorial 3))

     

    ํ˜น์‹œ๋ผ๋„ main์—์„œ ์ถœ๋ ฅํ•ด์•ผํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ  ๊ณ„์‹œ๊ฑฐ๋‚˜ main ๋ฌธ์„ ์‚ฌ์šฉํ•˜์‹œ๋Š” ๋ถ„๋“ค์€ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”...ใ…Žใ…Ž...

     

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

    [Haskell] 5 - 3. $ (function application)  (0) 2020.02.16
    [Haskell] #5 Type (2)  (0) 2020.01.20
    [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