Function, Monad, Arrow

  1. f :: Int -> (Int, Int)
  2. f = \x ->
  3. let y = 2 * x
  4. z1 = y + 3
  5. z2 = y - 5
  6. in (z1, z2)
  7. -- ghci> f 10
  8. -- (23, 15)
  9. fM :: Int -> Identity (Int, Int)
  10. fM = \x -> do
  11. y <- return (2 * x)
  12. z1 <- return (y + 3)
  13. z2 <- return (y - 5)
  14. return (z1, z2)
  15. -- ghci> runIdentity (fM 10)
  16. -- (23,15)
  17. fA :: Int -> (Int, Int)
  18. fA = proc x -> do
  19. y <- (2 *) -< x
  20. z1 <- (+ 3) -< y
  21. z2 <- (subtract 5) -< y
  22. returnA -< (z1, z2)
  23. -- ghci> fA 10
  24. -- (23,15)

24 Days of GHC Extensions: Arrows

ArrowZero, ArrowPlus, ArrowChoice, ArrowApply

  1. class Arrow a => ArrowZero a where
  2. zeroArrow :: a b c
  3. class ArrowZero a => ArrowPlus a where
  4. (<+>) :: a b c -> a b c -> a b c
  5. class Arrow a => ArrowChoice a where
  6. left :: a b c -> a (Either b d) (Either c d)
  7. left = (+++ id)
  8. right :: a b c -> a (Either d b) (Either d c)
  9. right = (id +++)
  10. (+++) :: a b c -> a b' c' -> a (Either b b') (Either c c')
  11. f +++ g = left f >>> arr mirror >>> left g >>> arr mirror
  12. where
  13. mirror :: Either x y -> Either y x
  14. mirror (Left x) = Right x
  15. mirror (Right y) = Left y
  16. (|||) :: a b d -> a c d -> a (Either b c) d
  17. f ||| g = f +++ g >>> arr untag
  18. where
  19. untag (Left x) = x
  20. untag (Right y) = y
  21. class Arrow a => ArrowApply a where
  22. app :: a (a b c, b) c
  1. instance MonadPlus m => ArrowZero (Kleisli m) where
  2. zeroArrow = Kleisli (\_ -> mzero)
  3. instance MonadPlus m => ArrowPlus (Kleisli m) where
  4. Kleisli f <+> Kleisli g = Kleisli (\x -> f x `mplus` g x)
  5. instance ArrowChoice (->) where
  6. left f = f +++ id
  7. right f = id +++ f
  8. f +++ g = (Left . f) ||| (Right . g)
  9. (|||) = either
  10. instance Monad m => ArrowChoice (Kleisli m) where
  11. left f = f +++ arr id
  12. right f = arr id +++ f
  13. f +++ g = (f >>> arr Left) ||| (g >>> arr Right)
  14. Kleisli f ||| Kleisli g = Kleisli (either f g)
  15. instance ArrowApply (->) where
  16. app (f,x) = f x
  17. instance Monad m => ArrowApply (Kleisli m) where
  18. app = Kleisli (\(Kleisli f, x) -> f x)
  1. Prelude Control.Arrow> runKleisli (Kleisli (\x -> [x * 2]) <+> Kleisli (\x -> [x, -x])) 2
  2. [4,2,-2]
  3. Prelude Control.Arrow> either (+2) (*3) (Left 3)
  4. 5
  5. Prelude Control.Arrow> either (+2) (*3) (Right 3)
  6. 9
  7. Prelude Control.Arrow> (+2) ||| (*3) $ (Left 3)
  8. 5
  9. Prelude Control.Arrow> (+2) +++ (*3) $ (Left 3)
  10. Left 5
  11. Prelude Control.Arrow> (+2) ||| (*3) $ (Right 3)
  12. 9
  13. Prelude Control.Arrow> (+2) +++ (*3) $ (Right 3)
  14. Right 9
  15. Prelude Control.Arrow> left (+2) (Left 3)
  16. Left 5
  17. Prelude Control.Arrow> right (*3) (Right 3)
  18. Right 9
  19. Prelude Control.Arrow> left (+2) (Right 3)
  20. Right 3
  21. Prelude Control.Arrow> right (*3) (Left 3)
  22. Left 3
  23. Prelude Control.Arrow> runKleisli (Kleisli (\x -> [x * 2]) ||| Kleisli (\x -> [x, -x])) (Left 3)
  24. [6]
  25. Prelude Control.Arrow> runKleisli (Kleisli (\x -> [x * 2]) ||| Kleisli (\x -> [x, -x])) (Right 3)
  26. [3,-3]
  27. Prelude Control.Arrow> runKleisli (Kleisli (\x -> [x * 2]) +++ Kleisli (\x -> [x, -x])) (Left 3)
  28. [Left 6]
  29. Prelude Control.Arrow> runKleisli (Kleisli (\x -> [x * 2]) +++ Kleisli (\x -> [x, -x])) (Right 3)
  30. [Right 3,Right (-3)]
  31. Prelude Control.Arrow> (first (+) >>> app) (1,2)
  32. 3
  33. Prelude Control.Arrow> (second (-) >>> snd &&& fst >>> app) (1,2)
  34. 1

Arrow 含义

How to read arrow combinators

Haskell语言学习笔记(47)Arrow(2)的更多相关文章

  1. Haskell语言学习笔记(88)语言扩展(1)

    ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...

  2. Haskell语言学习笔记(79)lambda演算

    lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...

  3. Haskell语言学习笔记(69)Yesod

    Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...

  4. Haskell语言学习笔记(20)IORef, STRef

    IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...

  5. Haskell语言学习笔记(39)Category

    Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...

  6. Haskell语言学习笔记(40)Arrow(1)

    Arrow class Category a => Arrow a where arr :: (b -> c) -> a b c first :: a b c -> a (b, ...

  7. Haskell语言学习笔记(92)HXT

    HXT The Haskell XML Toolbox (hxt) 是一个解析 XML 的库. $ cabal install hxt Installed hxt-9.3.1.16 Prelude&g ...

  8. Haskell语言学习笔记(72)Free Monad

    安装 free 包 $ cabal install free Installed free-5.0.2 Free Monad data Free f a = Pure a | Free (f (Fre ...

  9. Haskell语言学习笔记(49)ByteString Text

    Data.ByteString String 是 [Char] 的同义词,在使用上存在List的惰性所带来的性能问题. 在处理大型二进制文件时,可以使用 ByteString 来代替 String. ...

随机推荐

  1. mysql的一些 参数查询

    1 查询 事务 超时时间: SHOW GLOBAL VARIABLES LIKE 'innodb_lock_wait_timeout'; (默认innodb引擎事务的超时时间) 2 查询事务隔离级别 ...

  2. telinit:Did not receive a reply.Possible causes include:the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired

    问题: Enabling /etc/fstab swaps: [ok]telinit:Did not receive a reply.Possible causes include:the remot ...

  3. C#继承基本控件实现自定义控件 (转帖)

    自定义控件分三类: 1.复合控件:基本控件组合而成.继承自UserControl 2.扩展控件:继承基本控件,扩展一些属性与事件.比如继承Button 3.自定义控件:直接继承自Control 第一种 ...

  4. Microsoft Dynamics CRM 4.0导入组织(Import Organization)时间过长的原因总结

    952934    How to move the Microsoft Dynamics CRM 4.0 deployment http://support.microsoft.com/default ...

  5. phper必知必会之数组指针(四)

    数组指针 1.介绍几个数组指针的函数 current() - 返回数组中的当前单元 end() - 将数组的内部指针指向最后一个单元 prev() - 将数组的内部指针倒回一位 reset() - 将 ...

  6. linux vnc 安装

    http://blog.csdn.net/mchdba/article/details/49306383

  7. MVC文件图片ajax上传轻量级解决方案,使用客户端JSAjaxFileUploader插件01-单文件上传

    前段时间做了几个关于图片.文件上传的Demo,使用客户端Query-File-Upload插件和服务端Badkload组件实现多文件异步上传,比如"MVC文件上传04-使用客户端jQuery ...

  8. AIX6.1 线程模型说明

    引文:线程模型(Threading Model)默认从进程域 (M:N 模型 ) 改为系统全局域 (1:1 模型 ) 在 AIX 5L 中,pthread 线程的默认模型是 m:n 方式,而从 AIX ...

  9. 解决在sass中使用calc不能包含变量的问题。

    今天写sass的时候,发现在sass中使用calc,如果calc中包含一个变量,不会产生效果,看代码: .app-inner { display: flex; height: calc(100% - ...

  10. 【EasyUI学习-3】Easyui tabs入门实践

    作者:ssslinppp       1. 摘要 一般我们在设计程序主框架的时候,当点击(子)菜单时,希望相应界面都在tabs页中显示: 在显示的时候,如果之前打开过该界面,则希望重新选中对应的tab ...