第4回 スタート Haskell2 第9章

816a29f09828a8bd30302fac9161d6a4?s=47 mizu__tama
September 09, 2012
330

第4回 スタート Haskell2 第9章

816a29f09828a8bd30302fac9161d6a4?s=128

mizu__tama

September 09, 2012
Tweet

Transcript

  1. ୈ9ষ ΋ͬͱೖྗɺ΋ͬͱग़ྗ !NJ[V@@UBNB 12೥9݄9೔೔༵೔

  2. ΋ͬͱೖྗɺ΋ͬͱग़ྗ wϑΝΠϧ wίϚϯυϥΠϯҾ਺ wཚ਺ wCZUFTUSJOH 12೥9݄9೔೔༵೔

  3. ϑΝΠϧͱετϦʔϜ w)BTLFMMͰετϦʔϜσʔλΛ ؆୯ʹѻ͏ํ๏ w஗Ԇ*0ͷಈ࡞ 12೥9݄9೔೔༵೔

  4. ೖྗͷϦμΠϨΫτ import Control.Monad import Data.Char main = forever $ do

    l <- getLine putStrLn $ map toUpper l Q HIDNBLFDBQTMPDLFS DBQTMPDLFSIBJLVUYU 12೥9݄9೔೔༵೔
  5. ೖྗͷϦμΠϨΫτ import Control.Monad import Data.Char main = forever $ do

    l <- getLine putStrLn $ map toUpper l Q HIDNBLFDBQTMPDLFS DBQTMPDLFSIBJLVUYU 12೥9݄9೔೔༵೔
  6. HFU$POUFOUT HFU$POUFOUT*04USJOH import Data.Char main = do contents <- getContents

    putStr $ map toUpper contents w ඪ४ೖྗ͔Β&0'จࣈʹୡ͢Δ·Ͱ ͢΂ͯͷจࣈΛಡΈࠐΉ w ஗Ԇ*0Λߦ͏ Q 12೥9݄9೔೔༵೔
  7. HFU$POUFOUT HFU$POUFOUT*04USJOH import Data.Char main = do contents <- getContents

    putStr $ map toUpper contents w ඪ४ೖྗ͔Β&0'จࣈʹୡ͢Δ·Ͱ ͢΂ͯͷจࣈΛಡΈࠐΉ w ஗Ԇ*0Λߦ͏ Q ΠέͯΔʂ 12೥9݄9೔೔༵೔
  8. ஗Ԇ*0 import Data.Char main = do contents <- getContents putStr

    $ map toUpper contents Q DBQTMPDLFSIBJLVUYU ·ͩจࣈྻ͸ ͍Βͳ͍ΑͶ ࠷ऴతʹ͸จࣈྻʹධՁ͞ΕΔϓϩϛε ϝϞϦ 12೥9݄9೔೔༵೔
  9. ஗Ԇ*0 import Data.Char main = do contents <- getContents putStr

    $ map toUpper contents Q DBQTMPDLFSIBJLVUYU ·ͩ·ͩɺ ಡΈࠐ·ͳ͍Α ೖྗͷ݁ՌʹUP6QQFSΛϚοϓ͢Δϓϩϛε ϝϞϦ 12೥9݄9೔೔༵೔
  10. ஗Ԇ*0 import Data.Char main = do contents <- getContents putStr

    $ map toUpper contents Q DBQTMPDLFSIBJLVUYU ୺຤͔Βͷ ೖྗͷঢ়گ͸ʁ ϝϞϦ ೖྗͷ݁ՌʹUP6QQFSΛϚοϓ͢Δϓϩϛε ϝϞϦ େจࣈԽ͞Εͨ ߦ͕ඞཁͩΑʂ 12೥9݄9೔೔༵೔
  11. ஗Ԇ*0 import Data.Char main = do contents <- getContents putStr

    $ map toUpper contents Q DBQTMPDLFSIBJLVUYU ϝϞϦ I'm a lil' teapot What's with that airplane food, huh? It's so small, tasteless [EOF] IBJLVUYU 12೥9݄9೔೔༵೔
  12. JOUFSBDU JOUFSBDU 4USJOH4USJOH *0 main = do contents <- getContents

    putStr (shortLinesOnly contents) shortLinesOnly :: String -> String shortLinesOnly = unlines . filter (\line -> length line < 10) . lines  ೖྗΛจࣈྻͱͯ͠ड͚औΔ  ड͚औͬͨจࣈྻΛؔ਺Ͱม׵͢Δ  ݁ՌΛग़ྗ͢Δ ͱ͍͏Α͋͘ΔύλʔϯΛ؆୯ʹࡁ·ͤΔ Q 12೥9݄9೔೔༵೔
  13. JOUFSBDU JOUFSBDU 4USJOH4USJOH *0 main = interact shortLinesOnly shortLinesOnly ::

    String -> String shortLinesOnly = unlines . filter (\line -> length line < 10) . lines  ೖྗΛจࣈྻͱͯ͠ड͚औΔ  ड͚औͬͨจࣈྻΛؔ਺Ͱม׵͢Δ  ݁ՌΛग़ྗ͢Δ ͱ͍͏Α͋͘ΔύλʔϯΛ؆୯ʹࡁ·ͤΔ Q 12೥9݄9೔೔༵೔
  14. ϑΝΠϧͱετϦʔϜ w)BTLFMMͰετϦʔϜσʔλΛ ؆୯ʹѻ͏ํ๏ w஗Ԇ*0ͷಈ࡞ ͋Δೖྗʹରͯ͠ͲΜͳग़ྗ͕ߟ͑ΒΕΔ͔ͱ͍͏ ࢹ఺͔Βߟ͑ͯɺͦͷม׵Λ͢Δؔ਺Λॻ͘ 12೥9݄9೔೔༵೔

  15. ϑΝΠϧͷಡΈॻ͖ w ϑΝΠϧΛ։͖ɺϋϯυϧΛ֫ಘͯ͠ ϑΝΠϧʹରͯ͠Կ͔Λߦ͏  PQFO'JMF XJUI'JMF  I(FU$POUFOUT 

    SFBE'JMF XSJUF'JMF BQQFOE'JMF w Ұ࣌ϑΝΠϧΛ࢖͏  PQFO5FNQ'JMF  SFNPWF'JMF SFOBNF'JMF w ࣦഊͯ͠΋ϑΝΠϧΛด͡Δ  CSBDLFU  CSBDLFU0O&SSPS 12೥9݄9೔೔༵೔
  16. PQFO'JMF PQFO'JMF'JMF1BUI*0.PEF*0)BOEMF import System.IO main = do handle <- openFile

    “baabaa.txt” ReadMode contents <- hGetContents handle putStr contents hClose handle w 'JMF1BUIɹ4USJOHͷܕγϊχϜ w *0.PEF  3FBE.PEF  8SJUF.PEF  "QQFOE.PEF  3FBE8SJUF.PEF Q 12೥9݄9೔೔༵೔
  17. XJUI'JMF XJUI'JMF'JMF1BUI*0.PEF  )BOEMF*0B *0B import System.IO main = do

    withFile “baabaa.txt” ReadMode $ \handle -> do contents <- hGetContents handle putStr contents  'JMF1BUIͱ*0.PEF͔Β)BOEMFΛಘΔ  )BOEMFΛؔ਺ʹ౉͢  ϑΝΠϧϋϯυϧΛด͡Δ  ؔ਺͕ฦͨ͠*0ΞΫγϣϯΛฦ͢ Q 12೥9݄9೔೔༵೔
  18. XJUI'JMF XJUI'JMF'JMF1BUI*0.PEF  )BOEMF*0B *0B import System.IO main = do

    withFile “baabaa.txt” ReadMode $ \handle -> do contents <- hGetContents handle putStr contents  'JMF1BUIͱ*0.PEF͔Β)BOEMFΛಘΔ  )BOEMFΛؔ਺ʹ౉͢  ϑΝΠϧϋϯυϧΛด͡Δ  ؔ਺͕ฦͨ͠*0ΞΫγϣϯΛฦ͢ Q ࣦഊͨ͠ ৔߹΋ʂ 12೥9݄9೔೔༵೔
  19. ϒϥέοτ  ԿΒ͔ͷϦιʔεΛ֫ಘ͢Δ  ֫ಘͨ͠Ϧιʔεʹରͯؔ͠਺Λద༻͢Δ  ϦιʔεΛղ์͢Δʢ࣮֬ʹʂʣ  ؔ਺͕ฦͨ͠*0ΞΫγϣϯΛฦ͢ CSBDLFU*0B

    B*0C  B*0D *0D withFile :: FilePath -> IOMode -> (Handle -> IO a) -> IO a withFile name mode f = bracket (openFile name mode) (\handle -> hClose handle) (\handle -> f handle) Q 12೥9݄9೔೔༵೔
  20. ϒϥέοτ  ԿΒ͔ͷϦιʔεΛ֫ಘ͢Δ  ֫ಘͨ͠Ϧιʔεʹରͯؔ͠਺Λద༻͢Δ  ϦιʔεΛղ์͢Δʢ࣮֬ʹʂʣ  ؔ਺͕ฦͨ͠*0ΞΫγϣϯΛฦ͢ CSBDLFU*0B

    B*0C  B*0D *0D withFile :: FilePath -> IOMode -> (Handle -> IO a) -> IO a withFile name mode f = bracket (openFile name mode) (\handle -> hClose handle) (\handle -> f handle) Q 12೥9݄9೔೔༵೔
  21. ϒϥέοτ  ԿΒ͔ͷϦιʔεΛ֫ಘ͢Δ  ֫ಘͨ͠Ϧιʔεʹରͯؔ͠਺Λద༻͢Δ  ϦιʔεΛղ์͢Δʢ࣮֬ʹʂʣ  ؔ਺͕ฦͨ͠*0ΞΫγϣϯΛฦ͢ CSBDLFU*0B

    B*0C  B*0D *0D withFile :: FilePath -> IOMode -> (Handle -> IO a) -> IO a withFile name mode f = bracket (openFile name mode) (\handle -> hClose handle) (\handle -> f handle) Q 12೥9݄9೔೔༵೔
  22. ϒϥέοτ  ԿΒ͔ͷϦιʔεΛ֫ಘ͢Δ  ֫ಘͨ͠Ϧιʔεʹରͯؔ͠਺Λద༻͢Δ  ϦιʔεΛղ์͢Δʢ࣮֬ʹʂʣ  ؔ਺͕ฦͨ͠*0ΞΫγϣϯΛฦ͢ CSBDLFU*0B

    B*0C  B*0D *0D withFile :: FilePath -> IOMode -> (Handle -> IO a) -> IO a withFile name mode f = bracket (openFile name mode) (\handle -> hClose handle) (\handle -> f handle) Q 12೥9݄9೔೔༵೔
  23. ϋϯυϧΛѲΕʂ w I(FU$POUFOUT͸ϑΝΠϧͷ ϋϯυϧʹରͯ͠ಈ࡞͢ΔHFU$POUFOUT  I(FU-JOF I1VU4US I1VU4US-O I(FU$IBS w

    ϑΝΠϧΛಡΈࠐΈɺ ͦͷίϯςϯπΛจࣈྻͱͯ͠ѻ͏  SFBE'JMF  XSJUF'JMF  BQQFOE'JMF 12೥9݄9೔೔༵೔
  24. SFBE'JMF SFBE'JMF'JMF1BUI*04USJOH import System.IO main = do contents <- readFile

    “baabaa.txt” putStr contents  ϑΝΠϧύεΛड͚औΔ  ͦͷϑΝΠϧΛಡࠐΉ  ͦͷ಺༰Λද͢จࣈྻΛฦ͢ *0ΞΫγϣϯΛฦ͢ Q 12೥9݄9೔೔༵೔
  25. XSJUF'JMF  ϑΝΠϧύεͱɺ ͦͷϑΝΠϧʹॻ͖ࠐΈ͍ͨจࣈྻΛड͚औΔ  ͦͷॻ͖ࠐΈΛߦ͏*0ΞΫγϣϯΛฦ͢ XSJUF'JMF'JMF1BUI4USJOH*0 import System.IO import

    Data.Char main = do contents <- readFile “baabaa.txt” writeFile “baabaacaps.txt” (map toUpper contents) Q ˞ࢦఆ͞ΕͨϑΝΠϧ͕͢Ͱʹଘࡏ͢Δ৔߹ɺ্ॻ͖͢Δ 12೥9݄9೔೔༵೔
  26. BQQFOE'JMF  ϑΝΠϧύεͱɺ ͦͷϑΝΠϧʹॻ͖ࠐΈ͍ͨจࣈྻΛड͚औΔ  ͦͷॻ͖ࠐΈΛߦ͏*0ΞΫγϣϯΛฦ͢ BQQFOE'JMF'JMF1BUI4USJOH*0 import System.IO main

    = do todoItem <- getLine appendFile “todo.txt” (todoItem ++ “\n”) Q ˞ࢦఆ͞ΕͨϑΝΠϧ͕͢Ͱʹଘࡏ͢Δ৔߹ɺ຤ඌʹ௥ه͢Δ 12೥9݄9೔೔༵೔
  27. PQFO'JMF XJUI'JMF SFBE'JMF XSJUF'JMF BQQFOE'JMF ϑΝΠϧૢ࡞ͷؔ਺·ͱΊ ࣗ༝౓͕ߴ͍ ࣗ෼Ͱॻ͘෦෼͕গͳ͍ ϑΝΠϧΛ։͍ͯ ϋϯυϧΛಘΔ

    ϋϯυϧΛ࢖ͬͯ ϑΝΠϧͷத਎Λ จࣈྻͱͯ͠औΓग़͢ ϑΝΠϧΛด͡Δ औΓग़ͨ͠จࣈྻʹ ର͢Δૢ࡞ 12೥9݄9೔೔༵೔
  28. PQFO'JMF XJUI'JMF SFBE'JMF XSJUF'JMF BQQFOE'JMF ϑΝΠϧૢ࡞ͷؔ਺·ͱΊ ࣗ༝౓͕ߴ͍ ࣗ෼Ͱॻ͘෦෼͕গͳ͍ ϑΝΠϧΛ։͍ͯ ϋϯυϧΛಘΔ

    ϋϯυϧΛ࢖ͬͯ ϑΝΠϧͷத਎Λ จࣈྻͱͯ͠औΓग़͢ ϑΝΠϧΛด͡Δ औΓग़ͨ͠จࣈྻʹ ର͢Δૢ࡞ 12೥9݄9೔೔༵೔
  29. PQFO'JMF XJUI'JMF SFBE'JMF XSJUF'JMF BQQFOE'JMF ϑΝΠϧૢ࡞ͷؔ਺·ͱΊ ࣗ༝౓͕ߴ͍ ࣗ෼Ͱॻ͘෦෼͕গͳ͍ ϑΝΠϧΛ։͍ͯ ϋϯυϧΛಘΔ

    ϋϯυϧΛ࢖ͬͯ ϑΝΠϧͷத਎Λ จࣈྻͱͯ͠औΓग़͢ ϑΝΠϧΛด͡Δ औΓग़ͨ͠จࣈྻʹ ର͢Δૢ࡞ 12೥9݄9೔೔༵೔
  30. PQFO'JMF XJUI'JMF SFBE'JMF XSJUF'JMF BQQFOE'JMF ϑΝΠϧૢ࡞ͷؔ਺·ͱΊ ࣗ༝౓͕ߴ͍ ࣗ෼Ͱॻ͘෦෼͕গͳ͍ ϑΝΠϧΛ։͍ͯ ϋϯυϧΛಘΔ

    ϋϯυϧΛ࢖ͬͯ ϑΝΠϧͷத਎Λ จࣈྻͱͯ͠औΓग़͢ ϑΝΠϧΛด͡Δ औΓग़ͨ͠จࣈྻʹ ର͢Δૢ࡞ 12೥9݄9೔೔༵೔
  31. UFNQϑΝΠϧΛ࢖͏ (tempName, tempHandle) <- openTempFile “.” “temp” hPutStr tempHandle newTodoItems

    hClose tempHandle removeFile “todo.txt” renameFile tempName “todo.txt” QɹҰ෦ൈਮ ϑΝΠϧͷத਎Λॻ͖׵͑Δ Ұ࣌ϑΝΠϧʹ৽͍͠಺༰Λॻ͖ࠐΈɺ ݩͷϑΝΠϧͱஔ͖׵͑Δ PQFO5FNQ'JMF 4ZTUFN*0 SFNPWF'JMF SFOBNF'JMF 4ZTUFN%JSFDUPSZ 12೥9݄9೔೔༵೔
  32. UFNQϑΝΠϧΛ࢖͏ (tempName, tempHandle) <- openTempFile “.” “temp” hPutStr tempHandle newTodoItems

    hClose tempHandle removeFile “todo.txt” renameFile tempName “todo.txt” QɹҰ෦ൈਮ ϑΝΠϧͷத਎Λॻ͖׵͑Δ Ұ࣌ϑΝΠϧʹ৽͍͠಺༰Λॻ͖ࠐΈɺ ݩͷϑΝΠϧͱஔ͖׵͑Δ PQFO5FNQ'JMF 4ZTUFN*0 SFNPWF'JMF SFOBNF'JMF 4ZTUFN%JSFDUPSZ ϓϩάϥϜͷҟৗऴྃͰɺҰ࣌ϑΝΠϧ͕࢒ͬͯ͠·͏ 12೥9݄9೔೔༵೔
  33. ΫϦʔϯΞοϓ CSBDLFU0O&SSPS*0B B*0C  B*0D *0D ໰୊͕ىͬͨ͜৔߹Ͱ΋Ұ࣌ϑΝΠϧ͕ ࣮֬ʹ࡟আ͞ΕΔ bracketOnError (openTempFile

    “.” “temp”) (\(tempName, tempHandle) -> do hClose tempHandle removeFile tempName) (\(tempName, tempHandle) -> do hPutStr tempHandle newTodoItems hClose tempHandle removeFile “todo.txt” renameFile tempName “todo.txt”) QɹҰ෦ൈਮ 12೥9݄9೔೔༵೔
  34. ΫϦʔϯΞοϓ CSBDLFU0O&SSPS*0B B*0C  B*0D *0D ໰୊͕ىͬͨ͜৔߹Ͱ΋Ұ࣌ϑΝΠϧ͕ ࣮֬ʹ࡟আ͞ΕΔ bracketOnError (openTempFile

    “.” “temp”) (\(tempName, tempHandle) -> do hClose tempHandle removeFile tempName) (\(tempName, tempHandle) -> do hPutStr tempHandle newTodoItems hClose tempHandle removeFile “todo.txt” renameFile tempName “todo.txt”) QɹҰ෦ൈਮ Τϥʔ͕ൃੜͨ͠ ৔߹ʹ͢΂͖͜ͱ 12೥9݄9೔೔༵೔
  35. ίϚϯυϥΠϯҾ਺ ϢʔβͷࢦࣔΛϓϩάϥϜʹ఻͑Δ ࠷ળͷํ๏ import System.Environment import Data.List main = do

    args <- getArgs progName <- getProgName putStrLn “The arguments are:” mapM putStrLn args putStrLn “The program name is” putStrLn progName Q 12೥9݄9೔೔༵೔
  36. EJTQBUDIؔ਺ dispatch :: String -> [String] -> IO () dispatch

    “add” = add dispatch “view” = view dispatch “remove” = remove main = do (command:argList) <- getArgs dispatch command argList add :: [String] -> IO () add [fileName, todoItem] = appendFile fileName (todoItem ++ “\n”) QɹҰ෦ൈਮ w ϓϩάϥϜʹෳ਺ͷػೳΛ࣋ͨͤΔ w ؆୯ʹػೳΛ௥ՃͰ͖Δ EJTQBUDIؔ਺͔Βద੾ͳؔ਺Λड͚औΓɺ ͦͷؔ਺ʹҾ਺Λ౉ͯ͠*0ΞΫγϣϯΛಘΔ 12೥9݄9೔೔༵೔
  37. ෆਖ਼ͳೖྗʹରԠ͢Δ dispatch :: String -> [String] -> IO () dispatch

    “add” = add dispatch “view” = view dispatch “remove” = remove dispatch command = doesntExist command doesntExist :: String -> [String] -> IO () doesntExist command _ = putStrLn $ “The “ ++ command ++ “command doesn’t exist” main = do (command:argList) <- getArgs dispatch command argList add :: [String] -> IO () add [fileName, todoItem] = appendFile fileName (todoItem ++ “\n”) add _ = putStrLn “The add command takes exactly two arguments” Q  12೥9݄9೔೔༵೔
  38. ϥϯμϜੑ w७ਮͳ)BTLFMM͸ཚ਺͕ۤखʁ wϥϯμϜੑ͸*0Ͱ֎͔Β࣋ͬͯ͘Δ SBOEPN 3BOEPN(FOH 3BOEPNB H B H 4ZTUFN3BOEPNϞδϡʔϧ

    SBOEPNT 3BOEPN(FOH 3BOEPNB H<B> SBOEPN3 3BOEPN(FOH 3BOEPNB   B B H B H SBOEPN3T 3BOEPN(FOH 3BOEPNB   B B H<B> 12೥9݄9೔೔༵೔
  39. ϥϯμϜੑͱ*0HFU4UE(FO import System.Random main = do gen <- getStdGen putStrLn

    $ take 20 (randomRs (‘a’, ‘z’) gen) Q ࣮ߦ͢Δͨͼʹҧ͏݁Ռ͕ಘΒΕΔ import System.Random main = do gen <- getStdGen putStrLn $ take 20 (randomRs (‘a’, ‘z’) gen) gen2 <- getStdGen putStrLn $ take 20 (randomRs (‘a’, ‘z’) gen2) Q ಉ͡จࣈྻ͕ճදࣔ͞ΕΔ 12೥9݄9೔೔༵೔
  40. ϥϯμϜੑͱ*0OFX4UE(FO import System.Random main = do gen <- getStdGen putStrLn

    $ take 20 (randomRs (‘a’, ‘z’) gen) gen' <- newStdGen gen2 <- getStdGen putStrLn $ take 20 (randomRs (‘a’, ‘z’) gen') putStrLn $ take 20 (randomRs (‘a’, ‘z’) gen2) Qվ w ݱࡏͷཚ਺δΣωϨʔλΛͭͷδΣωϨʔλʹ෼ׂ  ৽͍͠ཚ਺δΣωϨʔλ͕ಘΒΕΔ  άϩʔόϧδΣωϨʔλ͕ߋ৽͞ΕΔ 12೥9݄9೔೔༵೔
  41. CZUFTUSJOH w4USJOHΑΓޮ཰ྑ͘จࣈྻΛѻ͏ wਖ਼֨CZUFTUSJOHͱ஗ԆCZUFTUSJOH  %BUB#ZUF4USJOH  %BUB#ZUF4USJOH-B[Z wCZUFTUSJOHϞδϡʔϧͷؔ਺  %BUB-JTUͷؔ਺ͱΑ͘ࣅͨؔ਺

     4ZTUFN*0ͷؔ਺ͱಉ༷ͷؔ਺ ·ͣී௨ͷจࣈྻͰॻ͍ͯΈͯɺ ύϑΥʔϚϯεʹຬ଍Ͱ͖ͳ͚Ε͹CZUFTUSJOHͰॻ͖׵͑Δ 12೥9݄9೔೔༵೔
  42. ΋ͬͱೖྗɺ΋ͬͱग़ྗ wϑΝΠϧ wίϚϯυϥΠϯҾ਺ wཚ਺ wCZUFTUSJOH 12೥9݄9೔೔༵೔