- http://d.hatena.ne.jp/propella/20070807/p1 1:åºæ¬
- http://d.hatena.ne.jp/propella/20070807/p2 2:並置
- http://d.hatena.ne.jp/propella/20070808/p1 3:é¸æ
-- ãªããç¥ãããã¡ã« Arrow æµè¡ã£ã¦ããããªã®ã§è©¦ãã¦ã¿ãã -- http://www.cs.chalmers.se/~rjmh/afp-arrows.pdf ã«ããã¨ããã¤ã³ãã -- ãªã¼ã¹ã¿ã¤ã«ã« IO ãæ··ããã®ã Arrow ã ã¨èããã°ãããããã import Control.Arrow -- Arrow ã§ã¯é«éåã使ãã¾ããã®ã§æ £ããªãã¨ã¡ãã£ã¨æãããè¦ããã -- Arrow 㯠Monad ã¨åããã¯ã©ã¹ã ããHaskell ã§è¨ãã¯ã©ã¹ã¨ã¯ä»ã§ãã -- ã¤ã³ã¿ãã§ã¼ã¹ã®äºã®äºãªã®ã§ãè²ã ãªåã«ä¸ãããã Arrow ã£ã½ãæ§è³ª -- ã®äºãæããArrow ãã½ãæ§è³ªãæã¤ãã®ã¨ãã¦ã¯ãé¢æ°ã Kleisli -- (Monad ãã©ããããç©)çããããèªåã§ããå¿ãã¦ãã¾ãã®ã§ä½åº¦ãæ¸ -- ããã©ãé¢æ°ã® Arrow ã¨ã¯ãé¢æ°ã Arrow (is-a) ãªã®ã§ã¯ãªããé¢æ°ã® -- Arrow ã£ã½ãã®äºãè¨ããArrow ã®æ¼ç®å >>> ã®ä¸¡è¾ºã¯åã種é¡ã® Arrow -- ãããªãã¨ãããªããä¸çªç°¡åãªé¢æ°ã® Arrow ã§èª¬æãããé¢æ°ã® Arrow -- ã§ã¯ >>> ãé¢æ°åæ . ã®åå¾ãå ¥ãæ¿ããç©ã¨ãã¦ä½¿ãããã¤ãã¿ãã -- ã¨ãè¨ããæååã空ç½ã§åã£ã¦åèªãæ°ããã«ã¯ãããããarr ã¯é¢æ° -- ãã Arrow ãçæããã®ã«ä½¿ãç´æã ããé¢æ°ããã®ã¾ã¾ Arrow ã¨ã㦠-- 使ãæã¯ç¡ãã¦ãè¯ãã wc = arr words >>> arr length -- *Main> wc "Hello, I am Takashi" -- 4 -- 次ã«ããã¡ã¤ã«ãèªãã§æååãæ°ããã¨ããã®ããã£ã¦ã¿ããäºã¤ã® IO -- æä½ readFile 㨠print ã使ããIO æä½ãã Arrow ãä½ãã«ã¯ Kleisli -- åã使ã(èªã¿æ¹è¬)ããKleisli ãªãã¨ããã®ããã«æ¸ãã¨ãªãã¨ããã -- ã¢ããã® Arrow (Kleisli ã® Arrow)ãä½æããããrunKleisli ãªãã¨ãã -- ã®ããã«æ¸ãã¨ãªãã¨ãããã¢ãã(IO æä½) ãåãåºãäºãåºæ¥ãã -- main é¢æ°ã®çµæã¨ã㦠runKleisli ãè¿ã㨠>>> ã§ã¤ãªããã¢ããã® -- Arrow ãå®è¡ãããã wcFile = Kleisli readFile >>> arr wc >>> Kleisli print -- *Main> runKleisli wcFile "HowToArrow.hs" -- 125 -- ã¾ããåé åãããã®ã§æ¬¡ã®ããã«ãæ¸ãããããã wcFile2 = Kleisli readFile >>> arr (words >>> length) >>> Kleisli print -- *Main> runKleisli wcFile "HowToArrow.hs" -- 125 -- ãªã¹ãã«ãªã¹ãã¢ãããããããã«ãã¹ããªã¼ã ãã¡ã³ã¯ã·ã§ã³ã¨ãã -- Arrow ããã£ã¦ããªã¹ãæä½ãç´åã«ä¸¦ã¹ãäºãåºæ¥ãã newtype SF a b = SF {runSF ::[a]->[b]} instance Arrow SF where arr f = SF (map f) SF f >>> SF g = SF (f >>> g) -- ãããããªãä¾ã§ããããªã¹ããéé ã«ãã¦äºåããé¢æ°ãå®ç¾©ãå®éã« -- å®è¡ããã«ã¯ runSF ã§ãªã¹ãé¢æ°ãåãåºãå¿ è¦ãããã doubleReverse = SF reverse >>> arr (* 2) -- *Main> runSF doubleReverse [1..5] -- [50,40,30,20,10] -- ããã¾ã§ã¯é¢åãªã ã㧠Arrow ã®æå³ãç¡ããããªæ°ããããã©ãããã -- ãé£ãããªãã®ã§ã¢ããããã