ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Haskell] #2 List(2) Range, list comprehension
    ๊ณต๋ถ€/ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ 2020. 1. 12. 13:23

    Haskell์€ ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ, ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์‰ฝ๊ฒŒ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

    Range์™€ List Comprehension์„ ์•Œ์•„๋ณด์ฃ !

     

    ์ง€๋‚œํŽธ

    2020/01/11 - [๊ณต๋ถ€/ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ] - [Haskell] #1 List (1) ๊ธฐ๋ณธ์ ์ธ ์—ฐ์‚ฐ๋“ค

     

    ๊ฐ„๋‹จํ•˜๊ฒŒ Online IDE๋กœ ํ…Œ์ŠคํŠธํ•ด๋ณด๊ณ  ์‹ถ๋‹ค! ์•„๋ž˜์— ๋“ค์–ด๊ฐ€์…”์„œ ์ž…๋ ฅํ•ด๋ณด์…”๋„ ์žฌ๋ฐŒ์Šต๋‹ˆ๋‹ค

    https://repl.it/languages/haskell

    Range

    ์ฑ… ๋ฒˆ์—ญ์—์„œ๋Š” '๋ฒ”์œ„'๋ผ๊ณ  ๋ฒˆ์—ญ๋˜์–ด ์žˆ์—ˆ์ง€๋งŒ, ์›๋ฌธ์—์„œ ๋‚˜์˜ค๋Š” range๊ฐ€ ์กฐ๊ธˆ ๋” ์‰ฝ๊ฒŒ ๋‹ค๊ฐ€์˜ค๊ธฐ ๋•Œ๋ฌธ์— ์ด ์šฉ์–ด๋ฅผ ๋Œ€์‹  ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.

     

    ์ •์˜ํ•˜๊ธฐ

    [์‹œ์ž‘์›์†Œ .. ๋์›์†Œ]

    [1..20]  
    [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

    ['a'..'z']  
    "abcdefghijklmnopqrstuvwxyz"  

    ์กฐ๊ฑด์„ ์ฃผ๊ณ  ์‹ถ๋‹ค๋ฉด
    [์‹œ์ž‘์›์†Œ, ๋‘๋ฒˆ์งธ์›์†Œ .. ๋์›์†Œ]

    [2,4..20]  
    [2,4,6,8,10,12,14,16,18,20

    //๋‹จ, ์œ ํ•œ ์†Œ์ˆ˜์ ์—์„œ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ฃผ์˜! ๋ฌดํ•œ ๋Œ€๋กœ ๊ณ„์‚ฐ์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์›์น˜ ์•Š๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    [0.1, 0.3 .. 1]

    [0.1,0.3,0.5,0.7, 0.8999999999999999, 1.0999999999999999]

     

     

    ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ

     

    ๋ฌดํ•œ๋Œ€๋กœ ๊ณ„์‚ฐํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ทธ ๊ฐ’์„ ๋ช‡ ๊ฐœ ๊ฐ€์ ธ์˜ฌ ๊ฒƒ์ธ์ง€ ์ œ์•ฝํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. (์•ˆ ๊ทธ๋Ÿฌ๋ฉด ํ•„์š”์‹œ ๋ฌดํ•œ๋Œ€๋กœ ๊ณ„์‚ฐํ•˜๊ณ  ์žˆ์–ด์š”...)


    ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ[์‹œ์ž‘์›์†Œ..]

    take 10 [2..] //[2..] ๊ทธ๋ƒฅ ์ž…๋ ฅํ•˜์ง€ ๋งˆ์„ธ์š”... ๋ฌดํ•œ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋‹ˆ๊นŒ์š”ใ… 
    [2,3,4,5,6,7,8,9,10,11]

    ์กฐ๊ฑด์ด ์žˆ๋Š” ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ
    [์ฒซ๋ฒˆ์จฐ ์›์†Œ, ๋‘๋ฒˆ์งธ ์›์†Œ .. ]

    take 10 [2,4..]
    [2,4,6,8,10,12,14,16,18,20]

     

    ๋‚ด์šฉ์ด ๋ฐ˜๋ณต๋˜๋Š” ๋ฆฌ์ŠคํŠธ

    repeat, cycle, replicate์„ ์‚ฌ์šฉํ•ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    cycle

    ์ฃผ์–ด์ง„ ๋ฐฐ์—ด ๋‚ด ์›์†Œ๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

     

    take 10 (cycle "hello world")

    "hello worl"

    take 10 (cycle [1,2,3])

    [1,2,3,1,2,3,1,2,3,1]

     

    repeat

    ํ•œ ๊ฐ€์ง€ ์›์†Œ๋ฅผ ๋ฐ˜๋ณต ํ•ฉ๋‹ˆ๋‹ค. ๋“ค์–ด์˜จ ๊ฐ’์ด ๋ฆฌ์ŠคํŠธ๋ฉด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

    repeat  ๋ฐ˜๋ณตํ•  ๋ฆฌ์ŠคํŠธ


    take 10 (repeat "hello world")


    ["hello world","hello world","hello world","hello world","hello world","hello world","hello world","hello world","hello world","hello world"]

     

    replicate

    ๋ฐ˜๋ณต ํšŒ์ˆ˜, ๋ฐ˜๋ณตํ•  ๊ฐ’์„ ๋ฐ›์•„ ๊ทธ ํšŸ์ˆ˜ ๋งŒํผ๋งŒ ๋ฐ˜๋ณตํ•ด์ค๋‹ˆ๋‹ค.

     

    replicate ๋ฐ˜๋ณตํšŒ์ˆ˜ ๋ฐ˜๋ณตํ•  ๊ฐ’

    replicate 3 "hello world"

    ["hello world","hello world","hello world"]

     

    List Comprehension (๋ฆฌ์ŠคํŠธ ํ†ตํ•ฉ)

    ์ด๊ฒƒ์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ณ ,์ด๋™ํ•˜๊ณ  ํ•ฉ์น˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ด ์ค๋‹ˆ๋‹ค.

    ์ˆ˜ํ•™์—์„œ์˜ Set Comprehension(์กฐ๊ฑด ์ œ์‹œ๋ฒ•)๊ณผ ์œ ์‚ฌํ•˜๋‹ค๊ณ  ํ•˜๋Š”๋ฐ์š”, ํ ... ๊ธฐ์–ต์ด ๋‚˜์ง€ ์•Š๋„ค์š”. ์กฐ๊ฑด ์ œ์‹œ๋ฒ•์€ ์•„๋ž˜ ์ฒ˜๋Ÿผ ์ƒ๊ฒผ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

     

     

    ์ถœ์ฒ˜ : http://learnyouahaskell.com/starting-out#ready-set-go

    x๋Š” 10๊ณผ ๊ฐ™๊ฑฐ๋‚˜ ์ž‘์€ ์ž์—ฐ์ˆ˜ ์ด๊ณ , S๋Š” ์ด x์— 2๋ฅผ ๊ณฑํ•œ ์ง‘ํ•ฉ์ด๋ผ๋Š” ๋œป์ด๋ž๋‹ˆ๋‹ค. (์ˆ˜ํฌ์ž ๋ ˆ๋ชฌํ‚ด์€ ๋จธ๋ฆฌ๊ฐ€ ๋ฒŒ์จ ํ„ฐ์งˆ ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค)

     

    ๊ทธ๋Ÿผ ์ด์ œ ํ•˜์Šค์ผˆ์—์„œ๋Š” ์–ด๋–ป๊ฒŒ ํ•˜๋Š” ์ง€ ์•Œ์•„๋ณผ๊นŒ์š”?

     

    ์ •์˜ํ•˜๊ธฐ

    [ ํ‘œํ˜„ ํ•  ์‹ (๋ณ€์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒ ์ฃ ?) | ์‹์— ๋“ค์–ด๊ฐˆ ๋ณ€์ˆ˜๋“ค์˜ ์กฐ๊ฑด ๋ฆฌ์ŠคํŠธ ]

    //์กฐ๊ฑด์ด 1๊ฐœ ์ผ ๋•Œ
    [ 2 * x  | x <- [1..10] ]

    [2,4,6,8,10,12,14,16,18,20]

    //์กฐ๊ฑด์ด 2๊ฐœ. ์ด ์ถ”๊ฐ€๋˜๋Š” ์กฐ๊ฑด์„ predicate (์„œ์ˆ ๋ถ€)๋ผ๊ณ  ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.
    [ x | x <- [50..100], x `mod` 7 == 3]  

    [52,59,66,73,80,87,94]

    //๋ณ€์ˆ˜๊ฐ€ 2๊ฐœ
     [x + y | x <- [1,2], y <- [3]]


    //ํ•จ์ˆ˜์˜ ์ž…๋ ฅ๊ฐ’์œผ๋กœ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    length' list = sum [ 1 | _ <- list] // _๋Š” ๊ฐ’์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๋•Œ ์ž„์‹œ ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    length' [1,2,4]
    3

     

    if ๋ฌธ์ด ๋“ค์–ด๊ฐ„ Comprehension

     


    boomBangs xs = [ if x < 10 then "BOOM!" else "BANG!" | x <- xs, odd x] //์˜ˆ์ œ๋Š” ์ฑ…์—์„œ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค. 

    boomBangs [1..13]


    ["BOOM!","BOOM!","BOOM!","BOOM!","BOOM!","BANG!","BANG!"]

     

    ์ด์ค‘ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋“ค์–ด๊ฐ„ List Comprehension

     

    //๋ฌธ์ž์—ด๋„ char์˜ ๋ฆฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค!
    [ x ++ " " ++ y | x <- ["hello", "good after noon", "good night"] , y<- ["lemon kim" , "stranger"] ] 


    ["hello lemon kim","hello stranger","good after noon lemon kim","good after noon stranger","good night lemon kim","good night stranger"]

     

    ํ™•์‹คํžˆ ํ•˜์Šค์ผˆ์—์„œ ๋ฆฌ์ŠคํŠธ ์—ฐ์‚ฐ์ด ์ฐธ ํŽธ๋ฆฌํ•œ ๊ฒƒ ๊ฐ™์•„์š”! ํŠนํžˆ ์กฐ๊ฑด์„ ์ค˜์„œ ์ƒ์„ฑํ•˜๋Š” ๋ถ€๋ถ„์ด ํŽธํ•˜๋„ค์š”.

    Swift์—์„œ์˜ ๋ฌธ๋ฒ•๊ณผ ๋น„์Šทํ•œ ๋ถ€๋ถ„์ด ์žˆ์–ด์„œ ๋ฐ˜๊ฐ‘๊ธฐ๋„ ํ•˜๊ตฌ์š”ใ…‹ใ…‹

     

    ๋‹ค์Œ์—๋Š” Tuple์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ธฐ๋กœ ํ• ๊ฒŒ์š”! (์ฑ…์˜ ๊ตฌ์กฐ๋Œ€๋กœ ๋”ฐ๋ผ๊ฐ€๋Š” ์ค‘์ž„๋‹ค ใ…Žใ…Ž)

    ์˜ค๋Š˜๋„ ์ข‹์€ ํ•˜๋ฃจ ๋ณด๋‚ด์„ธ์š”!

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

    [Haskell] #5 Type (2)  (0) 2020.01.20
    [Haskell] #4 Type (1)  (0) 2020.01.19
    [Haskell] #3 Tuple  (0) 2020.01.12
    [Haskell] #1 List (1) ๊ธฐ๋ณธ์ ์ธ ์—ฐ์‚ฐ๋“ค  (0) 2020.01.11
    [Haskell] #0 Hello, Haskell!  (0) 2020.01.09

    ๋Œ“๊ธ€

Designed by Tistory. @Bola Kim