One Div Zero: Monads are Elephants Part 1(ã¢ããã¯è±¡ã )ã¨ããã¨ã³ããªããã£ãã®ã§ãããã訳ãã¦ã¢ããã®å¦ç¿ãé²ãã¾ãã Introductions to monads are bit of cottage industry on the Internet. ã¢ããã®ç´¹ä»ã¯ã¤ã³ã¿ã¼ãããã§ã®ã»ãã®å°ããªäºæ¥ã§ãã So I figured, "why buck tradition?" ãªã®ã§ç§ã¯ããªãæ £ç¿ãç ´ãã®ãï¼ãã¨æãã®ã§ãã But this article will present Scala's way of dealing with monads. ãããããã®è¨äºã§ã¯Scalaã§ã¢ãããæ±ãæ¹æ³ã示ãã¾ãã An ancient parable goes that several blind men were experiencin
大æSIãã³ãã«ã¦SEãPMãã¢ã¼ããã¯ãã¨ãã¦å¤åããã®ã¡ï¼æ ªå¼ä¼ç¤¾è±èµãçµã¦ï¼ç¾å¨ã¯ååä¼ç¤¾ã·ã³ãã«ã¢ã¼ããã¯ã代表社å¡ã§ããï¼æ ªå¼ä¼ç¤¾å Business Placeã®ãã¼ãã³ã³ãµã«ã¿ã³ãã主ã«è¶ ä¸æµã®ããã»ã¹ã§ããè¦æ±éçºããªãã¸ã§ã¯ãæåï¼ã¢ã¸ã£ã¤ã«éçºã®ã³ã³ãµã«ã¿ã³ãã¨ãã¦æ´»èºä¸ãéçºã®ç¾å ´ã«ãã ããï¼éçºã®ç¾å ´ãå°ãã§ããããããã¨æ¥å¤å¥®éãã¦ãããè¦æ±éçºã¢ã©ã¤ã¢ã³ã¹å·è¡å§å¡ãèæ¸ã«ããªãã¸ã§ã¯ãè³ã®ã¤ããæ¹ãããeXtreme Programmingå®è·µã¬ãã¼ããï¼ã¨ãã«ç¿æ³³ç¤¾çºè¡ãå¾è ã¯å ±èï¼ãªã©ãããã Javaãªã©ï¼ãªãã¸ã§ã¯ãæåãæç¶ãåã®ããã°ã©ãã³ã°ã®çµé¨ã¯ããããã©ï¼é¢æ°åã®ããã°ã©ãã³ã°ã¯åãã¦ã¨ããçæ§ã®ããã®ï¼ããã¦çè èªèº«ã®ããã®ãé¢æ°è³ã®ã¤ããæ¹ãã·ãªã¼ãºã®Second Seasonï¼First Seasonã¯ãã¡ãï¼ãä»åã¯ãã¢ããããåãä¸ãã¾
Haskell ã®ãªã¹ãå å 表è¨ã¯ã¨ã£ã¦ã便å©ã§ãããã¾ãæå³ããªãã®ã§ãããããåºãããä¾ã¯ããããªæãã§ãã [(x,y)|x<-[1,2],y<-[3,4,5]] â [(1,3),(1,4),(1,5),(2,3),(2,4),(2,5)] ãã®ããã«ããã®ãªã¹ãå å 表è¨ã¯ãããããäºéã®ã«ã¼ãã§ãããã®ããã«åãã¾ãã ãªã¹ãå å 表è¨ã¯ãå®ã¯ç³è¡£æ§æã§ãããdo ã«ç´ãã¨ä»¥ä¸ã®ããã«ãªãã¾ãã do x<-[1,2] y<-[3,4,5] return (x,y) â [(1,3),(1,4),(1,5),(2,3),(2,4),(2,5)] åã¯ããã®æå³ããã£ã¨ç解ã§ãã¾ããã§ããã "<-" ã¯ãã¢ããã¨ããç®±ã®ä¸ãããä¸èº«ãåãåºãã¾ãããã¨ãã°ãJust "str" ããä¸èº«ãåãåºã㨠"str" ã¨ãªãããã«ãMaybe ã¢ãããç解ããã®ã¯ç°¡åã§ãã ã§ãããªã¹ã
ã¾ãã¾ãè¡ã£ã¦ãããååï¼è«çã»ã©ã ãã»åï¼ãã¿ã¤ãã«ã«ãåãã¨ãããªãããå®éã¯åã®è©±ãã»ã¨ãã©ã§ããªãã£ããã¨ãåãã¦ã¯ãã¾ã£ããæ°ã·ãªã¼ãºãç´°ããã¨ãããèæ¯ç¥èãªãã¦ãããããããªãï¼æ°å¦ã»ã¨ãã©ãã£ã¦ãªããï¼ã®ã§ãããã£ãç¯å²ã§æ¸ãã¨ãã¦ãããééãã¾ãã£ã¦ãå¯è½æ§å¤§ã å ¨ä½çã«æç§æ¸çã«é²ããããã¨æã£ã¦ããããã ããæªå±±ãããä½èª¿ä¸è¯ã§æ¹é転æãå½¢å¼çãªãã®ããããçµµããã¡ã¤ã³ã§é²ãããã¨ã«ãªã£ãã ã¨ããããã§ã第1åã®äºå®ã¯ããã§ãããã¾ãããï¼ã¾ããã¤ãããããªå³ã®æãæ¹åã®å®ç¾©ã¨å®ä¾ä¼æ©è¡åã®åã§éã¶ã³ã¬ã¯ã·ã§ã³ã»ãã¼ã¿å®éã¯ã3ã®éä¸ã¾ã§ï¼ä¼æ©ã¯ãã£ããï¼ï¼ã§çµäºãã¾ãã:-D ã¾ãããï¼ã¾ããã¤ã·ãªã¼ãºå ¨ä½ã®ç®æ¨ã¨ãã¦ã¯â¦åã®åºæ¬æ¦å¿µãå¦ã¶ ã¢ããã®å®ä¾ãããããç¥ãã¢ããæ³åã«æ £ããã¢ããã®ã¯ã©ã¤ã¹ãªåã®ä½ãæ¹ãç¥ãã¢ãããã¢ãã¤ãã§ãããã¨ãç解ãããããªã¨ã
æºåæ£é¸æ¥è¨ èããã¢ããè¬åº§ãã®1ãè¦ãªããããåæ¡ã®åã®11åã¨çããä¸æ¡ã®æ´æ°ããå®éã«åããã¦æ±ãã¦ã¿ã¾ãããanswersã§ã¯ãaãbãcã®çµåãã®å ããæ¡ä»¶ã«ããããããã®ã®ã¿è¿ãã¾ãã module Main (main) where import Control.Monad (guard) answers :: [] Int answers = do a <- [1..9] b <- [0..9] c <- [0..9] let n = a * 100 + b * 10 + c guard $ n == (a + b + c) * 11 return n main :: IO () main = print answers -- => [198]確ãã«ã¢ããã¯èãã¾ãã表é¢çã«ã¯ã²ããããªãã ãã©ãããããè£ã§ããããã«ã¯ããã£ã¦ããããããã¨ãã åç §: 第4å ãåãåº
ããªã¹ãã¢ããã®åä½åçãèãããã®ç¶ãã§ãguard ã®åä½åçãèãã¦ã¿ã¾ãã guard ã¯ããªã¹ãå å 表è¨ã§ã¯ããããªæãã«æ¸ãã¾ãã [x | x <- [1,2], x < 2] â [1] ããã do ã§æ¸ãç´ãã¨ããããªãã¾ãã do x <- [1,2] guard (x < 2) return x guard ã®å®ç¾©ã¯ãContorol.Monad ã®ä¸ã«ãã£ã¦ããããã風ã«ãªã£ã¦ãã¾ãã guard :: (MonadPlus m) => Bool -> m () guard True = return () guard False = mzero åã«ã¯ return () ãä½ãæå³ããã®ãããã£ã±ãåããã¾ããã§ããã do ãã >>= ã¸å¤å½¢ ä¸è¨ã® do ã >>= ã¸å¤å½¢ããã¨ãããªãã¾ãã [1,2] >>= (\x -> guard (x < 2)
ååã®ã¢ããã®ç´¹ä»ã«å¼ãç¶ãï¼ä»åã¯å ·ä½çãªã¢ããã®ä¾ã«ã¤ãã¦è¦ã¦ããããã¨æãã¾ãã ã¨ã¯ãã£ã¦ãï¼ä»åã¯ã¾ã IOã¢ããã¯åãä¸ãã¾ããããããã«ï¼Haskellã®ã¢ããã¯IOã¢ãããæèãã¦ä½ããã¦ããããï¼IOã¢ããã«ã¤ãã¦èª¬æããã»ãããã£ããããé¨åãããã¾ãã ãã ï¼IOã¢ããã¯Haskellã§ä½¿ããã¦ããæ§ã ãªæè¡ãçµã¿åããããã®ã«ãªã£ã¦ãã¾ãããããªãIOã¢ããã«ã¤ãã¦èª¬æããã¨ï¼ã©ããIOã¢ããã®æ¬è³ªã§ã©ããããã§ãªãã®ããããããªããªã£ã¦ãã¾ãã¾ããæè¡çãªè¦è«ã§ãããªããã®ãï¼IOã¢ããã®æ§è³ªã¨åéããã¦ãã¾ãå±éºæ§ãããã®ã§ãã ããããåé¡ãåé¿ããã«ã¯ï¼åºç¤çãªãã®ãã段ã ã¨ç©ã¿éãã¦ç解ãã¦ããã®ãããã§ããããååï¼ã³ã³ãããæã¤å¤ã«ã¯ãåãåºãå¯è½ãªå¤ãã¨ãåãåºãä¸å¯è½ãªå¤ããããã¨èª¬æãã¾ãããä»åã¯ï¼ãåãåºãå¯è½ãªå¤ãã®ã¿ãæã¤ã³ã³ããã§ããLi
ã¢ããã®ãã¹ã¦ Haskell ã«ãããã¢ããããã°ã©ãã³ã°ã®çè«ã¨å®è·µã«é¢ããå æ¬çã¬ã¤ã Version 1.1.0 ãã®ãã¥ã¼ããªã¢ã«ã¯ãã¢ããã®æ¦å¿µã¨ãã®é¢æ°ããã°ã©ãã³ã°ã«ãããå¿ç¨ã« ã¤ãã¦ãåä¸ç´ã® Haskell ããã°ã©ãã«ãããããããå©ç¨ä¾¡å¤ããããã㪠解説ããããã¨ãæ¨ã¨ãã¦ãã¾ããèªè 㯠Haskell ã«ãªãã¦ãããã¨ãåæ㨠ãã¾ãããã¢ããã«é¢ããçµé¨ã¯è¦æ±ãã¦ãã¾ããããã®ãã¥ã¼ããªã¢ã«ã¯ãå¤ ãã®é¡æãã«ãã¼ãã¦ãã¾ããå¾åã®ã»ã¯ã·ã§ã³ã§ã¯ãååã®é¡æãããç解ã ã¦ãããã¨ãåæã¨ãã¾ããé ããã£ã¦ãã¢ããããã°ã©ãã³ã°ãä¾ç¤ºãããã ã®ãµã³ãã«ã³ã¼ããããããç¨æããã¦ãã¾ããä¸èªã§ããã¹ã¦ã®é¡æãå¸åã ããã¨ããã®ã¯ãå§ãã§ãã¾ããã ãã®ãã¥ã¼ããªã¢ã«ã¯ 3 ã¤ã®é¨åã§æ§æããã¦ãã¾ããæåã®é¨åã¯ã é¢æ°ããã°ã©ãã³ã°ã«ãããã¢ããã®åºæ¬ç
ã¡ã³ããã³ã¹
ãç¥ãã
é害
ãªãªã¼ã¹ãé害æ å ±ãªã©ã®ãµã¼ãã¹ã®ãç¥ãã
ææ°ã®äººæ°ã¨ã³ããªã¼ã®é ä¿¡
å¦çãå®è¡ä¸ã§ã
j次ã®ããã¯ãã¼ã¯
kåã®ããã¯ãã¼ã¯
lãã¨ã§èªã
eã³ã¡ã³ãä¸è¦§ãéã
oãã¼ã¸ãéã
{{#tags}}- {{label}}
{{/tags}}