伊斯利斯的哈斯克尔版本 - 注释(爆炸符号) [英] Haskell version of Idris !-notation (bang notation)

查看:140
本文介绍了伊斯利斯的哈斯克尔版本 - 注释(爆炸符号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近学习了一些Idris,有一件事情非常方便,就是!-notation,它让我缩短了一个do块中的monadic代码,比如

I've had the luxury of learning a bit of Idris lately and one thing I've found extremely convenient is the !-notation, which let's me shorten monadic code inside a do block such as

a' <- a
b' <- b
c' <- c
someFunction a' b' c'

更加美好

to the much nicer

someFunction !a !b !c

现在当我在Haskell中编写代码,我正在寻找类似的东西,但据我所知它并不存在(并且爆炸字符显然已用于严格的模式匹配)。有没有什么办法可以避免在do块中有一堆琐碎的左箭头?也许是一个扩展,增加了一个重写规则,或者是这个效果?

Now when I write code in Haskell, I'm looking for something similar but as far as I can tell it doesn't exist (and the bang character is obviously already used for strict pattern matching). Is there any way to avoid having a bunch of trivial left arrows inside a do block? Perhaps an extension that adds a rewriting rule, or something to that effect?

推荐答案

由于每个monad都是应用(GHC> = 7.10),我们可以写出

Since every monad is an Applicative (with GHC >= 7.10) we can write

someFunction <$> a <*> b <*> c

请注意,如果 someFunction 返回一个monadic类型 m T 的值,上面将返回 m(m T),这可能不是我们想要的正如@pigworker指出的那样)。然而,我们可以将加入这两层:

Note that if someFunction returns a monadic value of type m T, the above will return m (m T), which is likely not what we want (as @pigworker points out below). We can however join the two layers together:

join $ someFunction <$> a <*> b <*> c

这篇关于伊斯利斯的哈斯克尔版本 - 注释(爆炸符号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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