Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Haskell入門ハンズオン

Aruneko
July 07, 2018

 Haskell入門ハンズオン

オープンソースカンファレンス2018 Hokkaido (#osc18do) で発表したHaksell入門に関するハンズオンの資料です

Aruneko

July 07, 2018
Tweet

More Decks by Aruneko

Other Decks in Programming

Transcript

  1. )BTLFMMͬͯͲΜͳݴޠʁ w σϑΥϧτͰ஗ԆධՁ w ஋͕ਅʹඞཁʹͳΔ·ͰධՁ͞Εͳ͍ w ৔߹ʹΑͬͯ͸ϝϦοτͰɺ৔߹ʹΑͬͯ͸σϝϦοτ w ڧ͍੩తܕ෇͚ w

    ͍͍ͨͯͷ৔߹ܕਪ࿦ͷ͓͔͛ͰܕΛ໌ࣔ͢Δඞཁ͸ແ͍ w Ͱ΋ॻ͘ͱ͖͸ܕΛҙࣝͭͭ͠ॻ͘ͱॻ͖΍͍͢ w ॲཧͷࣦഊ΍*0ͳͲ΋ܕͰද͢
  2. ()$JͰి୎ w Α͋͘Δதஔԋࢉࢠ͕ར༻Մೳ w     w )BTLFMMతʹ͸தஔؔ਺ͱݺͼ·͢

    w ΧοίͳͲ΋ͦͷ·· w ͨͩ͠ϚΠφεͷ஋͸ׅހΛ෇͚ͯ໌ࣔ w ಛఆͷ৚݅ͰύʔεΤϥʔʹͳΓ·͢ > 1 + 1 2 > (2 + 4) / 2 3.0 > 2 * 10 / 5 4.0 > 10 - 100 -90 > -10 + 20 10 > 20 - (-10) 30 > 20 - -10 Precedence parsing error
  3. ()$JͰ#PPMԋࢉ w ଞͷݴޠͱ͍͍ͩͨҰॹ w ൱ఆ͚ͩ஫ҙʂʂ > 'a' == 'a' True

    > 1 /= 1 False > True && False False > False || True True > not True False
  4. ؔ਺Λ࡞Δ w Ҿ਺Λεϖʔε۠੾ΓͰॻ͍ͯ͋͛Ε͹0, w )BTLFMM͸ࣜࢦ޲ͰͲΜͳؔ਺΋ৗʹ஋Λฦ͢ͷͰSFUVSO͸ෆཁ w ෆཁͱ͍͏͔ͦ΋ͦ΋SFUVSOΛॻ͚ͳ͍ w ࡾฏํͷఆཧΛॻ͍ͯΈΔ >

    calcDistance x1 y1 x2 y2 = sqrt $ (x1 - x2) ^ 2 + (y1 - y2) ^ 2 > calcDistance 0 0 1 1 1.4142135623730951 > calcDistance 0 0 1 (sqrt 3) 1.9999999999999998 a2 + b2 = c2
  5. ϦετΛ࡞Δ w Α͘ݟΔײ͡ͷϦςϥϧͰॻ͚Δ w ൣғ Ϩϯδ ੜ੒΋Մೳ ॱ൪͕͋Δ΋ͷͳΒԿͰ΋0,  w

    ॳظ஋ͱ൪໨ͷ஋ΛΧϯϚ۠੾ΓͰॻ͚͹౳ࠩ਺ྻ΋0, > l = [1, 3, 5, 7, 9] > l [1,3,5,7,9] > [1..10] [1,2,3,4,5,6,7,8,9,10] > ['a'..'g'] "abcdefg" > [1,4..20] [1,4,7,10,13,16,19]
  6. Ϧετૢ࡞ؔ਺ w ઌ಄ΛऔΓग़͢ w ઌ಄Ҏ֎ΛऔΓग़͢ w ຤ඌҎ֎ΛऔΓग़͢ w ຤ඌΛऔΓग़͢ >

    head [1..5] 1 > tail [1..5] [2,3,4,5] > init [1..5] [1,2,3,4] > last [1..5] 5
  7. ؔ਺ͷΧϦʔԽ w )BTLFMMͷؔ਺͸σϑΥϧτͰΧϦʔԽ͞ΕΔ w Ҿ਺Λऔͬͯʮ࢒ΓͷҾ਺ΛऔͬͯฦΓ஋Λฦؔ͢਺ʯΛฦ͢ w ؔ਺ͷ෦෼ద༻͕ՄೳʹͳΔ > :t gcd

    gcd :: Integral a => a -> a -> a > gcdOfFiveAnd = gcd 5 > :t gcdOfFiveAnd gcdOfFiveAnd :: Integral a => a -> a > gcdOfFiveAnd 13 1
  8. ߴ֊ؔ਺ w ؔ਺ΛҾ਺ʹऔΔؔ਺Λߴ֊ؔ਺ͱݺͿ w Ҿ਺ʹؔ਺Λ౉͢ͱ͖͸ϥϜμࣜΛ࢖͏ w Ͱ΋ΧϦʔԽ͞ΕΔ͜ͱΛࢥ͍ग़͢ͱ΋ͬͱศར > :t map

    map :: (a -> b) -> [a] -> [b] > map (\x -> mod 10 x) [1, 2, 3] [0,0,1] > map (mod 10) [1, 2, 3] [0,0,1]
  9. ߹੒ؔ਺ w ਺ֶͷ࣌ؒʹ΍ͬͨ͋ͷҾ਺ؔ਺ͷ߹੒ؔ਺͸ʮʯͰදݱ w ଟҾ਺ؔ਺͸෦෼ద༻ͯ͠Ҿ਺ؔ਺ʹ͢Ε͹߹੒Մೳ g(f(x)) = (g ∘ f)(x)

    > cos . sin $ pi 1.0 cos(sin(x)) = (cos ∘ sin)(x) > sum . map (+ 1) . filter (> 5) $ [3, 4, 5, 6, 7] 15
  10. ϥϜμࣜͰλϓϧΛड͚औΔ w λϓϧ͸Ҿ਺ʹऔΔͱ͖ʹύλʔϯϚονͰ෼ղՄೳ w Ͱ΋͜ͷ৔߹͸[JQ8JUIͨ͠΄͏͕؆ܿ > map (\ (x, y)

    -> x + y) $ zip [1..10] [1..10] [2,4,6,8,10,12,14,16,18,20] > zipWith (+) [1..10] [1..10] [2,4,6,8,10,12,14,16,18,20]