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. Spark机器配置计算

    ● Based on the recommendations mentioned above, Let's assign 5 core per executors => --executor-c ...

  2. C# 中的 enum(枚举) 类型使用例子

    一.需要根据数字获取中文名称,C# 代码里面出现if 或switch 判断语句,比如下面的类为test1.class //获取计算类型的值 string AggregateType = string. ...

  3. WCF揭秘学习笔记(5):WF定制活动

    WF(Windows Workflow Foundation,Windows工作流基础)为.NET提供了一种基于模型的.声明方式的过程执行引擎,它改变了传统的通过一行行编写代码来开发服务功能的方式. ...

  4. SQL 将非标准日期格式转换成标准格式,进行条件判断

    a.JLDate为非标准日期格式: 例: 2011-8-28 0:00:000011-8-28 0:00:000111-8-4 0:00:00 select CONVERT(varchar(50),C ...

  5. springMVC学习(9)-全局异常处理

    一.异常处理思路: 系统中异常包括两类:预期异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发.测试通过手段减少运行时异常的发生. 系统的da ...

  6. fiddler链接手机

    fiddler设置: 见python_request-上海悠悠 哪里有说 手机和window设置: 1,要把电脑防火墙关掉 2.手机访问http://ip:port安装Fiddler证书,特别注意IO ...

  7. [转][html][jquery]

    <!-- 强行修改 CSS --> $('a').css("cssText","background:#fff !important;color:#2d6dc ...

  8. 第7章 进程关系(5)_贯穿案例2:mini shell(2)

    5. 贯穿案例2:mini shell(2) (1)己经完成的功能:pwd.cd.exit命令 (2)阶段性目标: ①env.export.echo及其他命令 ②标准输入.输出重定向"> ...

  9. nodejs中https请求失败,无报错

    今天群里一位同学在做练习的时候,采用https例子: // curl -k https://localhost:8000/ const https = require('https'); const ...

  10. Python之部分基础知识点汇总

    1.三元运算(又称三目运算) 三元运算(又称三目运算),简单条件语句的简写    if a<b: A    else: B等价于:A if a<b else B 2.