ãã¤ã³ãããªã¼ã¹ã¿ã¤ã«å ¥é
Haskell ã«ã¯ãã¤ã³ãããªã¼ã¹ã¿ã¤ã«ã¨ããã®ãããã¾ãã
ä¾ãã°
foo x = f (g x)
ã¨ããä¸ã® x ã¨ããã®ãããã¤ã³ããã¨è¨ããããã§ãï¼åãæ示ãã¦ããªããã x ã®åã a->b ã ã£ããããå¯è½æ§ããããã©ãã®å¯è½æ§ã¯ç½®ãã¨ãã¦ï¼ãè¦ããã«å¤ã®ãã¨ã§ããã
ã§ããã®ãã¤ã³ããé¤ãã¦ããã°ã©ãã³ã°ããã®ããã¤ã³ãããªã¼ã¹ã¿ã¤ã«ã¨è¨ãã¾ãã
ãã®å ´åã
foo = f.g
ã¨ãªãã¾ãã
ã¨ãããã¨ã§ãããã¨ããããã³ã¼ãããã¤ã³ãããªã¼ã¹ã¿ã¤ã«ã§æ¸ããããã«è¨ç·´ãã¾ãããã
åºæ¬çã«ãæ¸ãã¦ãã°æ
£ããã®ã§ã©ãã©ãæ¸ãã¾ãããã
åºæ¬
åºæ¬çã« (.) é¢æ°ã使ãã¾ãã
foo x = f (g x) foo x = (f.g) x foo = f.g
ã§ãã
ã¾ãã(.) ã¯äºé æ¼ç®åãªã®ã§ããããé¢æ°å½¢å¼ã§æ¸ãã°ã
f.g = (.) f g
ã¨ãªããæ´ã«
= (f.) g = (.g) f
ã¨æ¸ããã¨ãã§ãã¾ãã
ããã¯åã«å¼ãå¤å½¢ããã ããªã®ã§ãããf (g x) ã®å½¢å¼ã«ããããã«ãããçµæ§ä½¿ãã¾ãã
ãã¨ãæåã®ä¾ã§ããã£ãããã£ã¦ãã¾ããã
foo x y = f x y
ãããªé¢æ°ããã£ãã¨ããy ã¯å·¦ã¨å³ã§ä¸¡æ¹ã¨ãå³ç«¯ã«ããã®ã§ãåãé¤ããã¨ãã§ãã¾ãã
foo x = f x
ã§ãããã« x ãåæ§ã«åãé¤ãã¾ãã
foo = f
ãããªãã¾ãã
ããã ãç¥ã£ã¦ãã°çµæ§é å¼µãã¾ãã
åé¡ï¼
ã¨ãããã¨ã§ããã¤ã解ãã¾ãããã
foo x y = f (g x y)
ã¾ã㯠y ãé¤ãããã¨ããèãã¾ãããã
g' = g x ã¨èããã°ç°¡åã«åããã¾ãã
-- g x ã g' ã¨ãã foo x y = f (g' y) where g' = g x -- f (g x) ã®å½¢ã«ãªã£ãã®ã§ . ã«ç´ã foo x = f.g' where g' = g x -- where ã®é¨åãåãé¤ã foo x = f.g x
ã¨ãªãã¾ãã
次㫠x ãé¤ãã¾ãããã
ããã¯
foo x = (f.) (g x)
ã§ããã¨èããã°ããåããã¾ãã
-- (f.) ã f' ã¨ãã foo x = f' (g x) where f' = (f.) -- f (g x) ã®å½¢ã«ãªã£ãã®ã§ . ã«ç´ã foo = f'.g where f' = (f.) -- where ã®é¨åãåãé¤ã foo = (f.).g
ãããªãã¾ãã
ã¤ã¾ã
foo x y = f (g x y)
ããã¤ã³ãããªã¼ã«ããã¨
foo = (f.).g
ã«ãªãã¾ãã
åé¡ï¼
ããã²ã¨ã¤ããããã£ã¦ããã¾ãããã
foo x y z = f x (g y z)
z ããåãé¤ãã¾ãã
-- f x ã f' ã«ãg y ã g' ã«ãã foo x y z = f' (g' z) where f' = f x g' = g y -- f (g x) ã®å½¢ã«ãªã£ãã®ã§ . ã«ç´ã foo x y = f'.g' where f' = f x g' = g y -- where ã®é¨åãå ã«æ»ã foo x y = f x.g y
次㫠y ã§ãã
-- å¼ãå¤å½¢ãã foo x y = (f x.) (g y) -- (f x.) ã f' ã¨ãã foo x y = f' (g y) where f' = (f x.) -- . ã«ãã foo x = f'.g where f' = (f x.) -- where ãæ»ã foo x = (f x.).g
æå¾ã« x ãé¤ãã¾ããããã¯ã¡ãã£ã¨å¼·æµããããã¾ããã
-- (f x.) ã f' ã¨ãã foo x = f'.g where f' = (f x.) -- å¼ãå¤å½¢ãã foo x = (.g) f' where f' = (f x.) -- (f x.) ãå¼å¤å½¢ãã foo x = (.g) f' where f' = (.) (f x) -- f' ã¯å¼æ°ãåãããã«ãã¦ã¿ã¾ããã foo x = (.g) (f' x) where f' x = (.) (f x) -- f' ã f (g x) ã®å½¢ã«ãªã£ã¦ããã®ã§ . ã«ç´ã foo x = (.g) (f' x) where f' = (.).f -- foo ã f (g x) ã®å½¢ã«ãªã£ã¦ããã®ã§ . ã«ç´ã foo = (.g).f' where f' = (.).f -- where ãæ»ã foo = (.g).((.).f) -- . æ¼ç®åã¯å³çµåãªã®ã§ç¡é§ãªæ¬å¼§ãåãé¤ã foo = (.g).(.).f
ã¨ãªãã¾ãã
flip ç»å ´
ãããé¢æ°ããããã°çµæ§é å¼µããã®ã§ããã
foo x y = f y x
ã¿ãããªã®ã«ãªãã¨å³ããã®ã§ãflip é¢æ°ã使ããã¨ã«ãã¾ãã
foo x y = flip f x y
ã¨ãªãã
foo = flip f
ã«ãªãã¾ãã
åé¡ï¼
flip ã使ã£ã¦èªç±ã«å¼æ°ã®é çªãå ¥ãæ¿ããããããã«ãªã£ã¦ããã¨ããæãã§ãã
foo x y z = f z y x -- f ã flip ã㦠z ãå¾ãã«ç§»åããã foo x y z = flip f y z x -- flip f y ã flip ã㦠z ãå¾ãã«ç§»åããã foo x y z = flip (flip f y) x z -- z ãåãé¤ã foo x y = flip (flip f y) x -- flip f y ã y' ã¨ãã foo x y = flip y' x where y' = flip f y -- y' ãå¾ãã«ããããã« flip ãã foo x y = flip flip x y' where y' = flip f y -- where ãæ»ã foo x y = flip flip x (flip f y) -- (flip flip x) ã f' ã«ã(flip f) ã g' ã«ç½®ãæãã㨠f' (g' y) ã«ãªãã®ã§ . ã«ç´ãã foo x = flip flip x.flip f -- ãã¨ã¯æ®éã«ãã¤ã³ãããªã¼ã«ãã foo x = (.flip f) (flip flip x) foo = (.flip f).flip flip
é å¼µã£ã¦ã¿ã
ç·´ç¿ã¨ãã¦ã4å¼æ°ã®é¢æ°ã®é çªãå ¨ãã¿ã¼ã³ç¶²ç¾ ãã¦ã¿ã¾ããã
data A = A data B = B data C = C data D = D test :: A -> B -> C -> D -> a test = undefined test1 :: A -> B -> D -> C -> a test1 = (flip.).test test2 :: A -> C -> B -> D -> a test2 = flip.test test3 :: A -> C -> D -> B -> a test3 = (flip.).flip.test test4 :: A -> D -> B -> C -> a test4 = (.flip flip).flip (.).test test5 :: A -> D -> C -> B -> a test5 = (.flip flip).flip (.).flip.test test6 :: B -> A -> C -> D -> a test6 = flip test test7 :: B -> A -> D -> C -> a test7 = (flip.).flip test test8 :: B -> C -> A -> D -> a test8 = flip.flip test test9 :: B -> C -> D -> A -> a test9 = (flip.).flip.flip test test10 :: B -> D -> A -> C -> a test10 = (.flip flip).flip (.).flip test test11 :: B -> D -> C -> A -> a test11 = (.flip flip).flip (.).flip.flip test test12 :: C -> A -> B -> D -> a test12 = (.test).flip flip test13 :: C -> A -> D -> B -> a test13 = (flip.).(.test).flip flip test14 :: C -> B -> A -> D -> a test14 = (.flip test).flip flip test15 :: C -> B -> D -> A -> a test15 = (flip.).(.flip test).flip flip test16 :: C -> D -> A -> B -> a test16 = (.flip flip).flip (.).(.test).flip flip test17 :: C -> D -> B -> A -> a test17 = (.flip flip).flip (.).(.flip test).flip flip test18 :: D -> A -> B -> C -> a test18 = (.test).(.).flip flip test19 :: D -> A -> C -> B -> a test19 = (.flip.test).(.).flip flip test20 :: D -> B -> A -> C -> a test20 = (.flip test).(.).flip flip test21 :: D -> B -> C -> A -> a test21 = (.flip.flip test).(.).flip flip test22 :: D -> C -> A -> B -> a test22 = (.(.test).flip flip).(.).flip flip test23 :: D -> C -> B -> A -> a test23 = (.(.flip test).flip flip).(.).flip flip main = putStrLn ""
ã©ããååæ©æ¢°ã«ãªã£ã¦ã²ãããå¤å½¢ãã¾ããã¾ããã
ãã£ã¨ç¶ºéºã«ç°¡ç´ã§ããã®ãããã®ãããããªãã§ããã©ãæ©æ¢°çã«ããã¨ãããªæãã§ãã
å ¨é¨èª¬æããã®ã¯è¾ãã®ã§ãé·ã test17 ã ãå¤å½¢ã®æ§åãè¦ã¦ã¿ã¾ãããã
test17 :: C -> D -> B -> A -> a test17 c d b a = test a b c d -- a test17 c d b a = flip test b a c d test17 c d b a = flip (flip test b) c a d test17 c d b a = flip (flip (flip test b) c) d a test17 c d b = flip (flip (flip test b) c) d -- b test17 c d b = flip flip d (flip (flip test b) c) test17 c d b = flip flip d (flip flip c (flip test b)) test17 c d b = f' (g' b) where f' = flip flip d g' b = flip flip c (flip test b) test17 c d = f'.g' where f' = flip flip d g' = flip flip c.flip test test17 c d = flip flip d.flip flip c.flip test -- d test17 c d = (.flip flip c.flip test) (flip flip d) test17 c = (.flip flip c.flip test).flip flip -- c test17 c = (.flip flip) (.flip flip c.flip test) test17 c = (.flip flip) (flip (.) (flip flip c.flip test)) test17 c = (.flip flip) (flip (.) ((.flip test) (flip flip c))) test17 c = f' (g' c) where f' = (.flip flip) g' c = flip (.) ((.flip test) (flip flip c)) test17 c = f' (g' c) where f' = (.flip flip) g' c = f'' (g'' c) where f'' = flip (.) g'' c = (.flip test) (flip flip c) test17 = f'.g' where f' = (.flip flip) g' = f''.g'' where f'' = flip (.) g'' = (.flip test).flip flip test17 = (.flip flip).flip (.).(.flip test).flip flip
ã¨ãªãã¾ãã
ã¾ã¨ã
ããã ãå¤å½¢ãã¦ãããªãããf ã g ã¨ãã£ãé¢æ°ã®å
·ä½çãªæå³ã«ã¤ãã¦ã¯ä¸åè¨åãã¦ã¾ããã
ã¤ã¾ããã¤ã³ãããªã¼ã«ããã«ããã£ã¦ãé¢æ°ã®å
·ä½çãªå¦çãªãã¦ç¥ãå¿
è¦ã¯ããã¾ããã
ããããã®é¢æ°ã®æå³ãåãããªãã¦ããã¨ãããããã¤ã³ãããªã¼ã«ãããã¨ã¯ã§ãã¾ãã
ã¨ãããã¨ã§é å¼µã£ã¦ã©ãã©ããã¤ã³ããããèªç±ã«ãã¦ããã¾ãããã
Q.ãªããã¤ã³ãããªã¼ã¹ã¿ã¤ã«ã§æ¸ãã®ã§ããï¼
A.ããã«ãã¤ã³ãããããã