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

コードで遊ぼう Haskell #2.0

YooWaan
November 25, 2015

コードで遊ぼう Haskell #2.0

YooWaan

November 25, 2015
Tweet

More Decks by YooWaan

Other Decks in Programming

Transcript

  1. ߏ੒ʹ͍ͭͯ લஔ͖ ϓϩάϥϛϯάݴޠ ϓϩάϥϛϯάύϥμΠϜ ࢥ૝/࿦ ߏ଄Խ OOP ؔ਺ܕ ৘ใՊֶ ݍ࿦

    τϨϯυ ͙͎͎͎͎͠ ෼͔Γқ͍ ༏͍͠ੈք આ໌͠ʹ͘͘ ఱࠃ΁ͷ֊ஈ HEVEN HELL
  2. CONTENTS ಺༰ ▸ ྺ࢙ ▸ ఆٛʗϓϩάϥϛϯάύϥμΠϜ ▸ ؔ਺ʗؔ਺߹੒ ▸ ஗ԆධՁ

    ▸ Ϟφυ
 
 ݁࿦ ؔ਺ܕݴޠ ؔ਺ϓϩάϥϛϯάͬͯʂʁ Haskellͷ͞ΘΓ͚ͩ…
  3. ࢀߟ·Ͱʹ ྺ࢙ ▸ 1930೥୅ɹϥϜμܭࢉ Alonzo Church
 ؔ਺ܕݴޠͷഎܠ ::ϥϜμܭࢉ
 ϥϜμܭࢉ͸ͦΕࣗମ͕νϡʔϦϯά׬શ
 ͭ·ΓɺͦΕࣗମ͕ສೳνϡʔϦϯάϚγϯ

    ▸ 1958೥ɹɹLISP
 Sࣜͱ͍͏γϯϓϧ͔ͭڧྗͳه๏
 ํݴɿScheme, Common Lisp, Clojure 1930೥୅ λܭࢉ 1958೥ LISP 1975೥ Scheme 1984೥ Common Lisp 1990೥ Haskell 1.0 େن໛ͳ ந৅తͳײ৮ ؔ਺ܕʹϥϜμܭࢉ ͱ͸ݴͬͯͳ͍
  4. ఆ͍ٛΖ͍Ζ ఆٛɿɿຊΑΓ ▸ ؔ਺͕ୈ̍ڃͷର৅Ͱ͋Δݴޠ ▸ ؔ਺ͷϦςϥϧ͕͋Δ ▸ ؔ਺Λ࣮ߦ࣌ʹੜ੒Ͱ͖Δ ▸ ؔ਺Λม਺ʹೖΕͯѻ͑Δ

    ▸ ؔ਺Λखଓ͖΍ؔ਺ʹҾ਺ͱͯ͠༩͑Δ͜ͱ͕Ͱ͖Δ ▸ ؔ਺Λखଓ͖΍ؔ਺ͷ݁Ռͱͯ͠ฦ͢͜ͱ͕Ͱ͖Δ
  5. ຊͱ͔WikipediaΑΓ ϓϩάϥϛϯάύϥμΠϜ ▸ ؔ਺ܕ ▸ ܕ ▸ ७ਮ ▸ ධՁઓུ

    ▸ ϞδϡϥϦςΟ ▸ ෭࡞༻ ▸ OOP ▸ ΦϒδΣΫτ͕૬ޓ ʹϝοηʔδͷ΍Γ औΓ ▸ ΧϓηϧԽ ▸ ܧঝ ▸ ϙϦϞϑΟζϜ ▸ ߏ଄Խ ▸ ػೳΛந৅Խͨ͠Ծ ૝ػց ▸ ֊૚Խͨ͠ந৅Խ ▸ ࣮૷Өڹ͸ಉҰ֊૚
  6. ༨ஊ ϓϩάϥϛϯάύϥμΠϜ ▸ ؔ਺ܕ ▸ ܕ ▸ ७ਮ ▸ ධՁઓུ

    ▸ ϞδϡϥϦςΟ ▸ ෭࡞༻ ▸ OOP
 
 
 
 
 
 
 
 ▸ ߏ଄Խ ݪଇ͸͋Δ͚Ͳ ΦϒδΣΫτͷൣғͬͯʁ ֊૚ͬͯͲΜͳ͘͘Γʁ
  7. ༨ஊ ϓϩάϥϛϯάύϥμΠϜ ▸ ؔ਺ܕ ▸ ܕ ▸ ७ਮ ▸ ධՁઓུ

    ▸ ϞδϡϥϦςΟ ▸ ෭࡞༻ ▸ OOP
 
 
 
 
 
 
 
 ▸ ߏ଄Խ ݁ہɺࣗ༝͗ͯ͢ɾɾɾ ʰෆ७ʱͰʰϞδϡϥϦςΟແ͘ʱͯʰ෭࡞༻ͩΒ͚ʱͳ ੈքΛߏங
  8. ϓϩάϥϛϯάαΠυ ؔ਺ ▸ ؔ਺ͷ࡞Γํ ▸ ؔ਺ద༻ʢ෦෼ద༻ʣ ▸ ؔ਺߹੒ ▸ ͦͷଞ

    ▸ ຊεϥΠυͰ͸লུ ͜͜Ͱѻ͏ؔ਺ʹ͍ͭͯ int y = 0; int func(int x) { y = y + 1; // ঢ়ଶมԽ return y + x // yʹґଘͯ݁͠Ռ͕มΘΔ } ʮ७ਮʯͰͳ͍ ʮϞδϡϥϦςΟʯͰͳ͍ ʮ෭࡞༻ʯ͕͋Δ ɹؔ਺Ͱ͸ͳ͍
  9. FUNCTION APPLICATION ؔ਺ద༻ ▸ ؔ਺ద༻ ▸ ؔ਺ͷҾ਺ʹ஋Λ༩͑Δ͜ͱ ▸ ෦෼ద༻ ▸

    ؔ਺ͷҰ෦͚ͩʹҾ਺Λ༩͑Δ ▸ ηΫγϣϯΛར༻ͯ͠Ұ෦Ҿ਺
 Λ༩͑Δ countChar :: String -> Char -> Int countChar str c = length $ filter (== c) str countA :: String -> Int countA str = countChar str ‘a' main = do printf "num %d\n" (countChar "a characters" 'a') printf "apply %d\n" (countA "a characters") printf "calc %d\n" ((2 ^) 4 :: Int) ؔ਺ʹҰ෦Ҿ਺Λ༩͑Δʢ෦෼ద༻ʣ ηΫγϣϯΛར༻ ؔ਺ద༻
  10. FUNCTION COMPOSITION ؔ਺߹੒ ▸ ఆٛҬ X ͔Β஋Ҭ Y ΁ͷ
 ͋Δؔ਺

    g ͱɺఆٛҬ Y ͔Β஋Ҭ
 Z ΁ͷ͋Δؔ਺ f ͕͋Δͱ͖
 ҎԼͷ౳ࣜΛຬͨ͢ఆٛҬ X ͔Β
 ஋Ҭ Z ΁ͷؔ਺ h ͕ଘࡏ͠·͢
 h (x) = f ( g ( x ) )
 
 
 h Λ f ◦ g ͱॻ͖fͱgͷؔ਺߹੒ʹ
 ΑΔ߹੒ؔ਺ͱݴ͍·͢
 (f ◦ g)( x ) = f( g ( x ) ) countChar :: String -> String -> Char -> String countChar fmt str c = printf fmt $ length $ filter (== c) str main = do putStrLn (countChar "a num %d" "a characters" 'a') putStrLn (countChar "c num %d" "a characters" 'c') X Y Z g f f ° g h ؔ਺߹੒
  11. ϓϩάϥϛϯάαΠυ ஗ԆධՁ ▸ ධՁઓུͷҰछͰ༩͑ΒΕͨࣜΛܭࢉ݁͠ՌΛಘΔɻ
 ධՁΛߦ͏ͱ͖ͷܭࢉॱংͷܾΊํͰ͢ɻ
 ࣮ࡍʹධՁ͕ඞཁͱ͞ΕΔ·ͰධՁΛߦΘͳ͍ઓུͰ͢ɻ emptyList :: Bool emptyList

    = let x = undefined : x in null x nullTake :: Int nullTake = let x = undefined : x in length (take 10 x) main = do print (take 10 [0..]) -- ແݶͷ഑ྻΛѻ͏ print (length [ undefined, undefined, undefined ]) -- ະఆٛͳ஋ͷ഑ྻ΋ѻ͑Δ print emptyList -- ۭϦετνΣοΫʹະఆٛΛ౉͢ print nullTake -- ະఆٛͳ஋Λ10ճऔಘ ஗ԆධՁͳͷͰແݶΛѻ͏͜ͱ΋Մೳ
  12. ϓϩάϥϛϯάαΠυ ஗ԆධՁ —- Haskell import System.Environment (getArgs) tarai :: Int

    -> Int -> Int -> Int tarai x y z | x <= y = y | otherwise = tarai (tarai (x - 1) y z) (tarai (y - 1) z x) (tarai (z - 1) x y) main = do args <- getArgs let nums = map cv args where cv str = read str :: Int if length nums >= 3 then print (tarai (nums !! 0) (nums !! 1) (nums !! 2)) else print "illegal argument !!" // C #include <iostream> #include <cstdlib> int tarai(int x, int y, int z) { return x <= y ? y :tarai(tarai(x - 1, y, z), tarai(y - 1, z, x), tarai(z - 1, x, y) ); } int main(int argc, char* argv[]) { if (argc < 4) return 1; int x = std::atoi(argv[1]); int y = std::atoi(argv[2]); int z = std::atoi(argv[3]); std::cout << tarai(x, y, z) << std::endl; return 0; } ./tarai 20 10 0 # ऴΘΒͳ͍ ./tarai 20 10 0 # ऴΘΔ
  13. Ϟφυ Ϟφυͷ͍Ζ͍Ζ ▸ Identity จ຺Λ࣋ͨͳ͍ ▸ Maybe ࣦഊͷՄೳੑΛ͍࣋ͬͯΔ ▸ List

    ෳ਺ͷՄೳੑΛ͍࣋ͬͯΔ ▸ Reader ࢀরͰ͖Δ؀ڥΛڞ༗͢Δ ▸ Writer ओཁͳܭࢉͷԣͰɺผͷ஋΋Ұ௚ઢʹ߹੒͢Δ ▸ State ঢ়ଶͷҾ͖ܧ͗ ▸ IO ෭࡞༻Λ൐͏
  14. ऴΘΓͷ࢝·Γ ݍ࿦ͱ͸ ‣ WikipediaΑΓ
 ݍ࿦ʢ͚ΜΖΜɺcategory theoryʣ͸ɺ਺ֶతߏ଄ͱͦͷؒͷؔ܎Λந৅తʹѻ͏਺ֶཧ࿦ͷ 1 ͭͰ͋Δɻ ߟ͍͑ͯΔछྨͷʮߏ଄ʯΛ࣋ͬͨର৅ͱͦͷߏ଄Λ൓ө͢ΔΑ͏ͳର৅ؒͷࣹͷू ·Γ͔ΒͳΔݍ͕جຊతͳߟ࡯ͷର৅ʹͳΔɻ

    ‣ ຊΑΓ
 ۙࣅͱͯ͠ɺݍ࿦ͱ͸ࣸ৅ͷͳ͢ʢந৅ʣ୅਺
 ΤΞϥϯήϯɾϓϩάϥϜ(Erlangen Program)ͷ఻౷ΛҾ͖ܧ͍ͰߟҊ͞ΕͨɺҰൠతͳ֓೦͸
 ”ߏ଄Λอͭม׵”ͷ֓೦ͷಛ௃͚ͮΛ༩͑ɺ͜ͷΑ͏ͳม׵Λڐ༰͢Δߏ଄ͷɺ͋Δछͷಛ௃ͮ ͚Λ༩͑Δ
  15. Φϫλ ݍͱ͸ ▸ Category(ݍ) = (O, A, ◦, id)
 ɾO͸ର৅


    ɾX,Y∈O ʹରͯ͠A(X,Y)͸ɺର৅X͔Βର৅Y΁ͷࣹͷू߹
 ɾ߃౳ࣹ X∈Oʹରͯ͠ idxɿX → X ͕ఆ·͍ͬͯΔ
 ɾ߹੒ɹ݁߹཯ɿ೚ҙͷࣹʹରͯ͠ h◦(g◦f) = (h◦g)◦f
 ɹ୯Ґ཯ɿ೚ҙͷࣹ X→Yʹରͯ͠ id y ◦ f = f = f ◦ id x X Y Z f g f ◦ g ʁʁʁ ʁʁʁ
  16. R ʘ(^O^)ʗ ·ͱΊΔͱ ݍ X (ෆมྔ) ݍ Y (ෆมྔ) P

    (ੑ࣭) Q (ੑ࣭) ؔख (ࣹ) ※ ద੾ͳߏ଄Λอͭؔखʹج͍ͮͯ
 දࣔతҙຯ࿦Λ༩͑ΒΕΕ͹ = දࣔతҙຯ࿦ ݍ࿦ ந৅౓͕ߴ͍=
  17. FUNC_R ʘ(^O^)ʗ ແཧ΍Γ·ͱΊΔͱ ݍ X (ෆมྔ) ݍ Y (ෆมྔ) FUNC_P

    FUNC_Q ؔख (ࣹ) ※ ద੾ͳߏ଄Λอͭؔखʹج͍ͮͯ
 දࣔతҙຯ࿦Λ༩͑ΒΕΕ͹ = Haskell ݍ࿦ ந৅౓͕ߴ͍= ◦