哈斯克尔重定向

发布于 2024-10-15 13:17:57 字数 328 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萧瑟寒风 2024-10-22 13:17:57

一种方法是定义一个中缀函数 (|>) ,它接受一个值和一个函数,并调用该函数并将其传递给该值,如下所示:

(|>) :: a -> (a -> b) -> b
(|>) x f = f x

然后您可以像在您的代码中一样使用它例子:

3 |> digitToInt

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:

(|>) :: a -> (a -> b) -> b
(|>) x f = f x

Then you can use it exactly like in your example:

3 |> digitToInt
轻拂→两袖风尘 2024-10-22 13:17:57

你可以按照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 get f =<< g =<< x (or (f <=< g) =<< x) and with applicatives you get f <$> 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.

傲性难收 2024-10-22 13:17:57

我不认为 Haskell 有一个标准的运算符(我可能是错的),但你当然可以定义一个。一个有点相关的概念是 $ 运算符,它允许您在编写一系列应用程序时去掉括号(这是在 F# 中使用 |> 的原因之一)。不同之处在于 $ 不会颠倒顺序:

f $ g $ h x  =  f (g (h x))

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:

f $ g $ h x  =  f (g (h x))

BTW: A fantastic way to find out things like this in Haskell is to use Hoogle. For example:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文