ãã¦ãæ°åãå¤ã㦠ArrowLoop ãããã¾ããè«æã«æ¸ãã¦ãã loop f b = let (c,d) = f (b,d) in c ã®æå³ãå ¨ãåãããªã㦠Arrow ã®åå¼·ãä¸ã¶æè¿ãåæ»ãã¦ããã®ã§ãããid:MaD ãããç´ æ´ããã解説 http://d.hatena.ne.jp/MaD/20070818 ãæ¸ããã¦ããã®ã§èªåãªãã«åçµãã¾ããloop ã使ã£ã¦æ®éã®ã«ã¼ããä½ãã«ã¯ãå¼æ°ã¨ãã¦é¢æ°ãä¸ã¤ä¸ãã¾ããé¢æ°ã®å㯠(b,d) -> (c,d) ã®ãããªå ¥åºåã¨ãã¿ãã«ã¨ãã¾ããããããå³ã§ç¤ºãã¨ãã¿ãã«ã®å¾ãã®å¼æ°ããããã¨å ¥åã«ç¹ãããå½¢ããã¦ãã¾ãã import Control.Arrow nonsense (b, d) = (b + 1, d * 2) -- *Main> loop nonsense 1 -- 2 -- ãã®ããããç¡ãä¾ã§ã¯ãb +
æ¨æ¥ã®ç¶ãã§ããè¨æ³ãããã¨ãMonadã¿ããã«å¯ä½ç¨ãæ±ããã¨ãè¨ã£ãã¡ãªãããã¯ããã«è¶ããArrowã®ç´ æ´ãããã«ã¤ãã¦ç´¹ä»ãã¾ãã reverse.reverse == id ã? èªåãæåã«Arrowã£ã¦ãããã¨æ°ã¥ããåé¡ãããã§ãã大å¦ã§åç´çã¨è©±ãã¦ããæã«æ°ã¥ãã¾ããã大ãã¦å½¹ã«ç«ã¤ä¾ã§ã¯ç¡ãã§ãããArrowã®æ°ããå´é¢ãè¦ãã¦ããä¾ã§ãã æ¨æºå ¥åã«å¯¾ãã¦reverse,reverse,headãé ã«å®è¡ãã¦çµæãåºåããã³ã¼ãã¯ä»¥ä¸ã®ããã«ãªãã¾ããå¾ã®æ¯è¼ã®çºã«æ¢ãã¦Arrowã§æ¸ãã¦ãã¾ãã import Control.Arrow main :: IO () main = getContents >>= print. (reverse >>> reverse >>> head) ãªã¹ããï¼åã²ã£ããè¿ãããå ã«æ»ãã ããªã®ã§ããã®headãã¨ãã¨ããè¨ç®ã¯ä¸
ãµã¨ãArrowã使ã£ã¦FizzBuzzãç°¡åã«æ¸ããããªã¨æã£ãã®ã§æ¸ãã¦ã¿ã¾ãããçµè«ããè¨ãã¨ããã¾ãæ¸ããããã¯ãªãã£ãã§ããã½ã¼ã¹ã³ã¼ããã¡ããif then elseã¬ãã«ã§èªåã§å®è£ ããã¦ããããã§ãã¦ãã©ã¤ãã©ãªãå å®ãã¦ããã°ãããªã«æ¸ãã«ãããã®ã§ããªãã¨ã¯æãã¾ãã æãã¬åç©«ã¨ãã¦Arrowç¨ã®mapã§ããmapAã使ç¨ããéã«ã¯ããã注æããªãã¨åé¡ãçºçãããã¨ãè¦ã¤ããã®ã§ããã«ã¤ãã¦ãæ¸ãã¾ãã ArrowChoiceã使ã æ®éã«å°ä½ã使ã£ã¦æ¸ãã¾ãããã£ããArrowã使ãã®ã§ã1,2,3,4,5,....ã¨ããã¹ããªã¼ã ããã£ã«ã¿ã¼ã«éãã¦ããã¨ããæãã§å®è£ ãã¾ããã ã¾ãå°ä½ã«ãããã£ã¦åå²ãå¿ è¦ãªã®ã§ãArrowChoiceã使ãã¾ããArrowChoiceã«ã¯4ã¤ã®æ¼ç®åãããã¾ãã ã¾ãleftã¨ããé¢æ°ããã£ã¦ãããã¯æ®éã®Arrowãå ¥å
ã¾ãæåã«ArrowLoopã«ã¤ãã¦èª¬æãã¾ããæ°å¦çãªè©±ã¯ä¸åãã¾ãã(ã§ãã¾ãã)ã ArrowLoopã®ä»çµã¿ ArrowLoopã¨ããã¯ã©ã¹ã«ã¯loopã¨ããé¢æ°ãä¸ã¤ã ãå±ãã¦ãã¾ãããã®loopã¯Arrowããæ°ããArrowãä½ãåºãã¦ãããé¢æ°ã§ãã ã¾ãloopã«å ¥ããArrowã¯ãããªå½¢ããã¦ãã¾ãã å ¥åã2ã¤ãããåºåã2ã¤ããã¾ãããã ããArrowã¯å ¥åã¨åºåãã©ã¡ããï¼ã¤ãã¤ã§ãªããã°ãªããªãã®ã§å®éã¯ã¿ãã«ã¨ãã¦å ¥åºåãã¾ããæ¨æºã©ã¤ãã©ãªã§å®ç¾©ããã¦ããArrowLoopã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ã«ã¯æ®éã®é¢æ°ã¨ãã¢ãããArrowåããKleisliã¨ããArrowãããã¾ãã ããã§ã¯ä¾ã¨ãã¦æ¬¡ã®Arrowãèãã¾ãã swap_mul = \(x, y) -> (2 * y, 3* x) å®è¡ä¾ > swap_mul (3, 4) (8,9)ç°¡åãªé¢æ°ã§ã
Programming with Arrowsãèªãã§ç解ããã¤ããã®ã¡ã¢ã誤ããªã©ä¹ããææã ï¼å¾©ç¿ï¼Arrowã£ã¦ãªã«ï¼ ã¨æã£ã¦ä»¥å調ã¹ãã¡ã¢ã"3åã§è§£ãHaskellã®Arrowã®åºæ¬ã¡ã¢ - ãããããã¾ãã"ãããã«ã¡ãã£ã¨è£è¶³ã¨ããã観ç¹ãå¤ãã¦ã¾ãæè¦ã®æ´çã Monadã«è²ããªç¨®é¡ãããããã«ãArrowãè²ããªç¨®é¡ãããã Monad: IOãMaybeã⦠Arrow: é¢æ°ãã®ã¾ãã¾(->)ãKleisli mã⦠ãã種é¡ã®Monadã«è²ããªåã®è²ããªå¤ãå ¥ããããããã«ããã種é¡ã®Arrowã«è²ããªåã®è²ããªé¢æ°ãå ¥ããããã Monad: Maybeã®ä¾â ãMaybe Intã ã«return 0ãreturn 777ãOKããMaybe Charã ã«return 'a'ãreturn ' 'ãOKã Arrow: (->)ã®ä¾â ãInt -> In
ä½ã FizzBuzz åé¡ãã¡ãã£ã¨ã ããã¯ã¼ã¢ããããããã®ã« FibBuzz ã¨ãããã®ãããããã§ã0 ãã 100 ã¾ã§ã®æ´æ°ã FizzBuzz ãã代ããã«ãã£ããããæ°ã®åé ã«å¯¾ã㦠FizzBuzz ããåé¡ãªããã§ãã é¢ç½ãããªã®ã§ä¹ ã ã« Control.Arrow ãå¼ã£å¼µãåºãã¦ãã£ã¦ã¿ã¾ããã ã©ã ãå¼ä½¿ãããã¤ã³ãããªã¼ã¹ã¿ã¤ã«ã§æ¸ãã¦ã¾ãããã®è¾ºã¯åºæ¬ã§ããã import Control.Arrow import Data.Function (on) fib = loop (((fst >>> uncurry (on (.) (:))) &&& (snd &&& fst >>> app) >>> app) &&& (snd >>> (snd &&& uncurry (+) >>>) >>> (uncurry (+) &&&) >>> (>>> uncurr
ã©ã³ãã³ã°
ã©ã³ãã³ã°
ã©ã³ãã³ã°
ãªãªã¼ã¹ãé害æ å ±ãªã©ã®ãµã¼ãã¹ã®ãç¥ãã
ææ°ã®äººæ°ã¨ã³ããªã¼ã®é ä¿¡
å¦çãå®è¡ä¸ã§ã
j次ã®ããã¯ãã¼ã¯
kåã®ããã¯ãã¼ã¯
lãã¨ã§èªã
eã³ã¡ã³ãä¸è¦§ãéã
oãã¼ã¸ãéã
{{#tags}}- {{label}}
{{/tags}}