哈斯克尔重定向
F# 工作让我了解了 Haskell。我目前正在阅读我强烈推荐的本教程的第 7 章。 不过问题很快。也许我有点超前了,我会在以后的章节中找到答案,但是,如果函数像 F# 中那样只接受一个参数,有没有一种方法可以反转函数及其参数的位置。 因此,例如,在 F# 中,如果您有一个名为 digitalToInt 的函数(就像在 Haskell 中所做的那样),您可以执行以下操作:
3 |> digitalToInt
我知道如何使用反引号,但那是专门针对二进制函数的。一元函数有类似的东西吗?
Working in F# has lead me to learn about Haskell. I'm currently on chapter 7 of this tutorial which I HIGHLY recommend.
Quick question though. Maybe I'm getting ahead of myself and I'll find the answer in future chapters but, is there a way to reverse the position of the function and it's arguments if the function only takes one argument like in F#.
So, for example, in F# if you had a function called digitToInt (as you do in Haskell), you could do the following:
3 |> digitToInt
I know about using back ticks, but that's specifically for binary functions. Is there anything similar for unary functions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种方法是定义一个中缀函数
(|>)
,它接受一个值和一个函数,并调用该函数并将其传递给该值,如下所示:然后您可以像在您的代码中一样使用它例子:
One way to do that could be defining an infix function
(|>)
that take a value and a function and calls the function passing it the value like this:Then you can use it exactly like in your example:
你可以按照peoro的建议去做,效果很好。不过,我建议不要这样做。 “阅读”Haskell 的正常方式就像一个句子——从左到右,动词在开头,主语在结尾。通常,您会得到像
f 这样的链式组合。 g。 q. r$x
。使用一元运算符,您可以得到f =<< g =<< x
(或(f <=< g) =<< x
),并且使用应用程序,您会得到f <$>; x<*> y* z 。因此,颠倒事物的正常顺序并不一定是惯用的。
顺便说一句,据我所知,F# 选择相反的运算符的原因是它与 Visualstudio 自动完成功能配合得非常好——如果您输入了正在操作的值,那么它的类型就可以确定选择可以对其进行操作的内容。这种工具支持非常棒,这将是采用替代风格的一个很好的论据。
You can do what peoro suggests and it works fine. However, I'd advise against it. The normal way to "read" Haskell is like a sentence -- left to right, with verbs at the beginning and the subject at the end. Typically you get a chained composition like
f . g. q . r $ x
. With monadic operators you getf =<< g =<< x
(or(f <=< g) =<< x
) and with applicatives you getf <$> x <*> y <*> z
. So reversing the normal order of things is not necessarily idiomatic.By the way, the reason, so I've heard, that F# chose the opposite operator is that it works very very nicely with visualstudio autocomplete -- if you've typed in the value you're operating on, then its type can determine choices for what can operate on it. That sort of tooling support is awesome, and it would be a good argument for adopting an alternate style.
我不认为 Haskell 有一个标准的运算符(我可能是错的),但你当然可以定义一个。一个有点相关的概念是
$
运算符,它允许您在编写一系列应用程序时去掉括号(这是在 F# 中使用|>
的原因之一)。不同之处在于$
不会颠倒顺序:BTW:在 Haskell 中查找此类内容的一个绝佳方法是使用 Hoogle。例如:
I don't think that Haskell has a standard operator for this (I may be wrong), but you can certainly define one. A somewhat related concept is the
$
operator that allows you to get rid of parentheses when writing a series of applications (which is one reason for using|>
in F#). The difference is that$
doesn't reverse the order:BTW: A fantastic way to find out things like this in Haskell is to use Hoogle. For example: