Function, Monad, Arrow

f :: Int -> (Int, Int)
f = \x ->
let y = 2 * x
z1 = y + 3
z2 = y - 5
in (z1, z2) -- ghci> f 10
-- (23, 15) fM :: Int -> Identity (Int, Int)
fM = \x -> do
y <- return (2 * x)
z1 <- return (y + 3)
z2 <- return (y - 5)
return (z1, z2) -- ghci> runIdentity (fM 10)
-- (23,15) fA :: Int -> (Int, Int)
fA = proc x -> do
y <- (2 *) -< x
z1 <- (+ 3) -< y
z2 <- (subtract 5) -< y
returnA -< (z1, z2) -- ghci> fA 10
-- (23,15)

24 Days of GHC Extensions: Arrows

ArrowZero, ArrowPlus, ArrowChoice, ArrowApply

class Arrow a => ArrowZero a where
zeroArrow :: a b c class ArrowZero a => ArrowPlus a where
(<+>) :: a b c -> a b c -> a b c class Arrow a => ArrowChoice a where
left :: a b c -> a (Either b d) (Either c d)
left = (+++ id) right :: a b c -> a (Either d b) (Either d c)
right = (id +++) (+++) :: a b c -> a b' c' -> a (Either b b') (Either c c')
f +++ g = left f >>> arr mirror >>> left g >>> arr mirror
where
mirror :: Either x y -> Either y x
mirror (Left x) = Right x
mirror (Right y) = Left y (|||) :: a b d -> a c d -> a (Either b c) d
f ||| g = f +++ g >>> arr untag
where
untag (Left x) = x
untag (Right y) = y class Arrow a => ArrowApply a where
app :: a (a b c, b) c
instance MonadPlus m => ArrowZero (Kleisli m) where
zeroArrow = Kleisli (\_ -> mzero) instance MonadPlus m => ArrowPlus (Kleisli m) where
Kleisli f <+> Kleisli g = Kleisli (\x -> f x `mplus` g x) instance ArrowChoice (->) where
left f = f +++ id
right f = id +++ f
f +++ g = (Left . f) ||| (Right . g)
(|||) = either instance Monad m => ArrowChoice (Kleisli m) where
left f = f +++ arr id
right f = arr id +++ f
f +++ g = (f >>> arr Left) ||| (g >>> arr Right)
Kleisli f ||| Kleisli g = Kleisli (either f g) instance ArrowApply (->) where
app (f,x) = f x instance Monad m => ArrowApply (Kleisli m) where
app = Kleisli (\(Kleisli f, x) -> f x)
Prelude Control.Arrow> runKleisli (Kleisli (\x -> [x * 2]) <+> Kleisli (\x -> [x, -x])) 2
[4,2,-2]
Prelude Control.Arrow> either (+2) (*3) (Left 3)
5
Prelude Control.Arrow> either (+2) (*3) (Right 3)
9
Prelude Control.Arrow> (+2) ||| (*3) $ (Left 3)
5
Prelude Control.Arrow> (+2) +++ (*3) $ (Left 3)
Left 5
Prelude Control.Arrow> (+2) ||| (*3) $ (Right 3)
9
Prelude Control.Arrow> (+2) +++ (*3) $ (Right 3)
Right 9
Prelude Control.Arrow> left (+2) (Left 3)
Left 5
Prelude Control.Arrow> right (*3) (Right 3)
Right 9
Prelude Control.Arrow> left (+2) (Right 3)
Right 3
Prelude Control.Arrow> right (*3) (Left 3)
Left 3
Prelude Control.Arrow> runKleisli (Kleisli (\x -> [x * 2]) ||| Kleisli (\x -> [x, -x])) (Left 3)
[6]
Prelude Control.Arrow> runKleisli (Kleisli (\x -> [x * 2]) ||| Kleisli (\x -> [x, -x])) (Right 3)
[3,-3]
Prelude Control.Arrow> runKleisli (Kleisli (\x -> [x * 2]) +++ Kleisli (\x -> [x, -x])) (Left 3)
[Left 6]
Prelude Control.Arrow> runKleisli (Kleisli (\x -> [x * 2]) +++ Kleisli (\x -> [x, -x])) (Right 3)
[Right 3,Right (-3)]
Prelude Control.Arrow> (first (+) >>> app) (1,2)
3
Prelude Control.Arrow> (second (-) >>> snd &&& fst >>> app) (1,2)
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. ASP.NET Core 2.0 使用支付宝PC网站支付实现代码(转)

    最近在使用ASP.NET Core来进行开发,刚好有个接入支付宝支付的需求,百度了一下没找到相关的资料,看了官方的SDK以及Demo都还是.NET Framework的,所以就先根据官方SDK的源码, ...

  2. 如何用Photoshop画一个发光金币(unity游戏素材教程)

    做好的发光金币预览图: 以下为如何用Photoshop画一个发光金币教程: [1]如上图1-2,新建,名称改为Coin,宽度20像素,高度20像素,分辨率72,背景白色: [2]使用Alt+Shift ...

  3. Python获取当前年月日

    import datetime datetime.datetime.now().year datetime.datetime.now().month datetime.datetime.now().d ...

  4. SecureCRT SSH主机秘钥配置文件管理

    选择Options(选项)->Global Options(全局选项)-> SSH HOST Keys

  5. ASP.NET Web Pages:页面布局

    ylbtech-.Net-ASP.NET Web Pages:页面布局 1.返回顶部 1. ASP.NET Web Pages - 页面布局 通过 Web Pages ,创建一个布局一致的网站是很容易 ...

  6. [原]System.IO.Path.Combine 路径合并

    使用 ILSpy 工具查看了 System.IO.Path 类中的 Combine 方法 对它的功能有点不放心,原方法实现如下: // System.IO.Path /// <summary&g ...

  7. 第10章 线程控制(5)_多线程下的fork

    6. 线程和fork 6.1 多线程下的fork (1)历史包袱 ①fork与多线程的协作性很差,这是POSIX系统操作系统的历史包袱. ②长期以来程序都是单线程的,fork运行正常,但引入线程这后, ...

  8. KuDu论文解读

    kudu是cloudera在2012开始秘密研发的一款介于hdfs和hbase之间的高速分布式存储数据库.兼具了hbase的实时性.hdfs的高吞吐,以及传统数据库的sql支持.作为一款实时.离线之间 ...

  9. python3中reduce()函数的使用方法示例

      reduce() 函数会对参数序列中元素进行累积,下面这篇文章主要给大家介绍了关于python中reduce()函数的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学 ...

  10. NFS各个版本之间的比较

    NFS是一种网络文件系统,从1985年推出至今,共发布了3个版本:NFSv2.NFSv3.NFSv4,NFSv4包含两个次版本NFSv4.0和NFSv4.1.经过20多年发展,NFS发生了非常大的变化 ...