ä»æ¥ã¾ã§ã«ç解ãã Haskell ã¨ã¢ããã«ã¤ãã¦ãã¾ã¨ãã¦ã¿ã¾ããééã£ã¦ããã¨ãããããã¨æãã¾ãã®ã§ãã³ã¡ã³ããæå¾ ãã¦ãã¾ãã(_ _)
Haskell ã®ç¹å¾´
ç´ç²é¢æ°åè¨èªã§ãã
- åç §ééæ§
- å¤æ°ã¯åæåã§ãã¾ãããä¸æ¦æ±ºã¾ã£ãå¤ã¯å¤æ´ã§ãã¾ãããé¢æ°ã®è¿ãçµæã¯ãå¼æ°ã®å¤ã ãã§æ±ºã¾ãã¾ãã
- é 延è©ä¾¡
- ãã¼ã¿ã®å¦çã¯ãæ¬å½ã«ãã¼ã¿ãå¿ è¦ã«ãªã£ãã¨ãã«å®è¡ããã¾ããUNIX ã®ãã¤ãã¿ãããªãã®ã ã¨èããã¨ããã§ãããã
Haskell ã«ãå¯ä½ç¨ã¯ããã¾ãã
ãã Lisper ã¯ãå¯ä½ç¨ã®ããé¢æ°ã¨ãªãé¢æ°ãåãã¦å®è£ ãã¾ããããããããã§ãªã Lisper ãåãã¦ãããã¨ã¯éãã¾ããã
Haskell ã使ãã¨ãå¯ä½ç¨ã®ããé¨åã¨ãªãé¨åãå¿ ç¶çã«åãã¦æ¸ããã¨ã«ãªãã¾ãã
å¤æ°ã®å¤ã¯å¤æ´ã§ããªã
ãã¨ãã°ãæå㪠quicksort ãä¾ã«ã¨ãã¾ãããã
quicksort (x:xs) = quicksort [ y | y <- xs, y <= x ] ++ [x] ++ quicksort [ y | y <- xs, y > x ]
å½ä»¤åè¨èªã®çºæ³ã§ã¯ãå ã ã®ãªã¹ãã®ä¸èº«ãç½®ãæããããã®ã ã¨æã£ã¦ãã¾ãã¾ãããããã次ã ã¨æ°ãããªã¹ããçæããã¦ããã®ã§ãã
ãã®ããã«ãå½ä»¤åã§ä»£å ¥ãããã¨æãã¨ããã§ã¯ãæ°ãããã¼ã¿ãä½ãã¨ããçºæ³ã®è»¢æãå¿ è¦ã§ãã
ã«ã¼ã
å¤æ°ã®å¤ãæ¸ãæããããªãã®ã§ãã¤ã³ããã¯ã¹ã使ã for æã¯ããã¾ãããã«ã¼ãã¯ãå帰ã§å®ç¾ãã¾ãã
fib 1 = 1 fib 2 = 1 fib n = fib (n - 1) + fib (n - 2) fib 6 â 8
ã¾ãããªã¹ãã®ãã¹ã¦ã®è¦ç´ ãæä½ãããã¨æãã®ã§ããã°ãååé¢æ°(map)ãå©ç¨ãã¾ãã
plus1 n = n + 1 map plus1 [1, 2, 3] â [2, 3, 4]
é 延è©ä¾¡
é 延è©ä¾¡ã§ã¯ãå®è¡é åºã決ã¾ããªãã®ã§ãIO åºåã®é çªã決ã¾ããªããã¨ã«ãªãã¾ããHaskell ã®èª¬æã«ã¯ãããããæç« ãæ£è¦ãã¾ãããå®éã«é çªã決ã¾ããªãä¾ã¯ç¤ºããã¦ãã¾ããã
ããã§å±±ä¸ããã«ãé çªã決ã¾ããªãä¾ãæãã¦é ãã¾ããã
以ä¸ã®ä¾ã§ã¯ãäºæ³éãã®é çªã§åºåããã¾ãããªããsequence_ ã¯ã(ã¢ããã®)ãªã¹ããåããè¦ç´ ãé ã«å®è¡ãã¦ãå¤ã¯è¿ããªãé¢æ°ã§ãã
sequence_ [ putStr "foo", putStr "bar" ] â "foobar" ã¨è¡¨ç¤º
以ä¸ã®ä¾ã§ã¯ãç´æçã«ã¯ã¾ããªã¹ããè©ä¾¡ãããã®ã§ããã㧠"foobar" ã¨åºåããããã®ãã¨ã«ãªã¹ããé転ãããããã«è¦ãã¾ãã
sequence_ (reverse [ putStr "foo", putStr "bar" ]) â "barfoo" ã¨è¡¨ç¤º
ãããã表示ãããã®ã¯ "barfoo" ã§ããããã¯ãreverse ããéã«ããªã¹ãã®è¦ç´ ãè©ä¾¡ããå¿ è¦ããªãããã§ãããªã¹ãã®è¦ç´ ã¯ãsequence_ ã使ãã¨ãã«ã¯ããã¦è©ä¾¡ããã¾ããã¤ã¾ããããã¯ä»¥ä¸ã®ããã«æ¸ãã®ã¨åãã«ãªãã¾ãã
sequence_ [ putStr "bar", putStr "foo" ] â "barfoo" ã¨è¡¨ç¤º
ã¢ããã¨ã¯ä½ãï¼
ç´ç²é¢æ°åè¨èªã§ã¯ã以ä¸ã®ãã¨ãåé¡ã«ãªãã¾ãã
- (ãã¨ãã° for æã® break ã®ãããª)å¶å¾¡æ§é ãã©ãå®ç¾ãããï¼
- (é 延è©ä¾¡ã«ãããããããã«)å®è¡é åºãæå®ããã«ã¯ã©ããããï¼
ãããã解決ããä»çµã¿ãã¢ããã§ãã
ã¢ããã¨ã¯ãå ·ä½çã«ã¯ããããã¼ã¿ã«ä»ããä»å æ å ±ãã§ããã©ãã«ã¨ãã¿ã°ã ã¨æã£ã¦ãããã§ããããã¢ããã§ã¯ãä»£å ¥æãªã©ã¯åå°å ¥ããã¾ãããããç´ç²é¢æ°åè¨èªã®ä¸çãå£ããã¨ã¯ããã¾ããã
ã¢ããã§ã¯ã2ã¤ã®é¢æ°ãå®ç¾©ããå¿ è¦ãããã¾ãã
return
ä»£å ¥ã§ããªãã¨ãããã¨ã¯ãç¾å¨å²ãå½ã¦ããã¦ããã¡ã¢ãªã®å 容ãæ¸ãæããããªãã¨ãããã¨ã§ããéã«è¨ãã°ãã¡ã¢ãªãæ°ãã«å²ãå½ã¦ã¦ä»å æ å ±ã¨ãã¦ãåé¡ãªãã§ããããé¢æ°ã§ããã許ãããé¨åã¯ãæ»ãå¤ã§ããã§ãããããã®é¢æ°ã®åå㯠return ã¨ããã¾ãã
ãã¨ãã°ãMaybe ã¢ããã使ãå ´åãæåå "foo" ã« return ãé©å¿ããã¨ãJust "foo" ã®ããã«ã©ãã«ãä»ãã¾ãã
-- Maybe ã®æè㧠return "foo" â Just "foo"
ã¢ããã«ãã£ã¦ã¯ãã©ãã«ãå¥ããã¦ä¸èº«ãåãåºãã¾ããMaybe ã¢ããã®ä¸èº«ãåãåºãé¢æ°ã¯ãfromJust ã§ãã
module Maybe fromJust(return "foo") â "foo"
>>=
bind ã¯ãå·¦ãå®è¡ãã¦ããå³ãå®è¡ãã¾ãã(bind ã®ä¸è¬çãªæ§è³ªãªã®ããMaybe/IO ã¢ããã® bind ã ãã®æ§è³ªãªã®ãåã£ã¦ãã¾ããã) ãã®ãããã§ãå®è¡é åºã決ã¾ãã¾ãã
ã¢ãã >>= f â æ°ããã¢ãã
å·¦ããå³ã¸è¡ããã¨ãåãããã«ããã®ãããªè¨å·ã«ãªã£ã¦ããã®ã§ãããã
3ã¤ã®ã¢ããåã®å 1 ã¤ãããã®æ§è³ªã表ãã¦ãã¾ãã
(return x) >>= f == f x
Maybe ã¢ãã
ããã°ã©ãã³ã°è¨èªä¸è¬ã§ã以ä¸ã®ãããªæ¡ä»¶å¼ãèãã¦ã¿ã¦ä¸ããã
a == "foo" && b == "bar" && c == "baz"
ã©ãã㧠false ã«ãªãã°ããã以éãè©ä¾¡ããã«ãå ¨ä½ã false ã«ãªãã¾ããããã¯ãä¸ç¨®ã®å¶å¾¡æ§é ã§ããfalse ã«ãªã£ãã break ãããã¨èãã¦ããã§ãããã
ãã®å¶å¾¡æ§é ã§ã¯ãå¤ãã®ãã®ãå¶å¾¡ã«ä½¿ããã¦ãã¾ããå¤ã¨å¶å¾¡æ å ±ãåããã«ã¯ã©ãããã°ããã§ããããï¼
Maybe ã¢ããã使ãã¨ãå¤ãä¿æããªãããã©ãã«ã«ãã£ã¦å¶å¾¡ãå®ç¾ãã¾ããå¥ã®è¨ãæ¹ãããã¨ãMaybe ã¢ããã¯ã失æãããããããªãæ¼ç®ãé£çµããããã«ä½¿ãã¾ãã
Monad ã®å®ç¾©ã¯ã以ä¸ã®ããã«ãªã£ã¦ãã¾ãã
data Maybe a = Nothing | Just a instance Monad Maybe where return = Just fail = Nothing Nothing >>= f = Nothing (Just x) >>= f = f x
ã©ãã«ã¯ãJust ã Nothing ã§ããJust ã trueãNothing ã false ã ã¨æã£ã¦ããã§ãããã>>= 㯠&& ã«ç¸å½ãã¾ãã
Maybe ã¢ããã®ä¸è¬çãªä½¿ç¨ä¾ã¨ãã¦ã¯ããã¼ã¿ãã¼ã¹ã®æ¤ç´¢ãæãããã¾ãããã¼ã¿ãã¼ã¹ãæ¤ç´¢ãããã¼ã¿ãããã°ãããã«ããã«å¯¾ãã¦å¥ã®æ¤ç´¢ããªããã°çµäºã¨ããå¶å¾¡ãå®ç¾ã§ãã¾ãã
db = [("alice", [("title", "Ms."), ("job", "sales")]), ("bob", [("title", "Mr."), ("job", "engineer")])] return db >>= lookup "bob" >>= lookup "job" â Just "engineer"
ãã¨ãã° "chris" ã§æ¤ç´¢ããã¨ãéä¸ã§æ¼ç®ãçµäºã Nothing ãè¿ãã¾ãã
return db >>= lookup "chris" >>= lookup "job" â Nothing
return 㨠>>= ã強調ããããã«ãããã風ã«æ¸ãã¾ãããã以ä¸ã®ããã«æ¸ãã¦ãæ§ãã¾ããã
lookup "bob" db >>= lookup "job"
ãããã¯å½ä»¤åè¨èªé¢¨ã«ãæ¸ãã¾ãã
do person <- lookup "bob" db lookup "job" person
ã"<-" ãã¢ããã®ä¸èº«ãåãåºãããã次ã®è¡ã«è¡ãã®ãå³ã®é¢æ°ãé©å¿ããããã¨ã«ç¸å½ããã¨èãã¦ããã§ãããã
IOã¢ãã
å ¥åºåã«ã¯ IO ã¨ããã©ãã«ãä»ãã¾ãã
ãã¨ãã°ãputStr ã¨ããæåååºåé¢æ°ã¯ä»¥ä¸ã®ããã«ä½¿ãã¾ãã
putStr "Hello, World"
return ã¨ã®é¢é£ãèããããã«ããããã return ã使ã£ã¦ã¿ãã¨ãããªãã¾ãã
return "Hello, World" >>= putStr
>>
åã種é¡ã®ã¢ããã¯ã">>" ã§é£çµã§ãã¾ãã
putStr ã¯1ã¤ã® IO ã¢ãããè¿ãã¾ããããputStr å士ã ">>" ã§é£çµã§ãã¾ãã
return "Hello, " >>= putStr >> return "World!" >>= putStr >> return "" >>= putStrLn
">>" ã®å·¦ã¨å³ã¨ã§ã¯ããã¼ã¿ã®ããã¨ãã¯ããã¾ãããå·¦ãå ã«è©ä¾¡ããã次ã«å³ãè©ä¾¡ããã¾ãããã®ãããåºåã®é çªãæå®ã§ãã¾ãã
return ãããã¨è¦ã«ããã®ã§ãæ¸ãæãã¦ã¿ã¾ãããã
putStr "Hello, " >> putStr "World!" >> putStrLn ""
ããã do ã使ã£ã¦å½ä»¤å風ã«æ¸ãç´ããã¨ãã§ãã¾ãã
do putStr "Hello, " putStr "World!" putStrLn ""
ããé¢æ°ã®åã« IO ãç¾ããªãå ´åããã®é¢æ°ã®ä¸ã§ putStr ãªã©ã使ããã¨ã¯ã§ãã¾ããããããã°ã®ããã®è¡¨ç¤ºã³ã¼ãã追å ãããã¨ã許ãããªãã®ã§ããããã«ãã£ã¦ãç´ç²é¢æ°åè¨èªã®ä¸çãå®ãã¾ãã