那箭头呢? [英] What about arrows?

查看:122
本文介绍了那箭头呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过阅读关于Haskell的各种类别主题类的各种教程,我们发现诸如 Monoid Functor Monad 等等 - 所有这些都有几十个实例。但出于某种原因,当我们到达 Arrow 时,只有两个实例:函数和monad。在这两种情况下,使用 Arrow 实例都不如直接使用底层事物更强大和更难。



有没有人有任何有趣的箭头的例子?我确信必须有一些,但我从来没有遇到任何有关他们的文章...

HXT 是一个用于解析XML的库,它是使用箭头的一个很好的例子(看看字箭头出现在这个包的模块名称中!)。你应该看看这个伟大的教程: http://adit.io/posts/2012-04- 14-working_with_HTML_in_haskell.html



但是对于函数使用箭头概念也很好。例如下面的代码:

 ((+ 1)&&(* 2))3  - 结果是(4,6)

只是起作用,因为( - >)是箭头类的一个实例(运算符&&& Control.Arrow )。



感谢箭头语法,您还可以在Haskell中编写复杂的计算工具(它对HXT中的函数,monad和XML过滤器也有效)。

Reading through various tutorials about Haskell's various category-themed classes, we find things like Monoid, Functor, Monad and so on - all of which have dozens of instances. But for some reason, when we reach Arrow, there are only two instances: functions and monads. In both cases, using the Arrow instance is less powerful and more difficult than just using the underlying thing directly.

Does anybody have any interesting examples of arrows? I'm sure there must be some, but I've never come across any writing about them...

解决方案

HXT, a library which is used for parsing XML, is a very good example for the usage of arrows (have a look how often the word Arrow occurs in the module names of this package!). You shall have a look on the great tutorial: http://adit.io/posts/2012-04-14-working_with_HTML_in_haskell.html

But it is also good to have the arrow concept for functions. For example the following code

((+1) &&& (*2)) 3 -- result is (4,6)

just works, because (->) is an instance of the arrow class (The operator &&& is defined in Control.Arrow).

Thanks to the arrow syntax you have also a great tool to write complex computations in Haskell (it works as well for functions, monads and XML filters in HXT).

这篇关于那箭头呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆