Y-Combinator 实际示例
我最近读了一些关于函数式编程的文章,并且正在尝试理解 Y-Combinator。 据我所知,您可以使用 Y-Combinator 以不直接支持递归的语言有效地实现递归。 然而,我可能使用的每种语言都已经支持递归,因此我不确定使用 Y-Combinator 对此有多大用处。
我缺少 Y-Combinator 使用的更好的实际示例吗? 有人在实际生产代码中实际使用过吗? 或者说使用 Y-Combinator 实际上只是一种令人费解的学术练习(尽管非常酷)。
I've been reading a bit lately about functional programming and I am trying to grok the Y-Combinator. I understand that you can use the Y-Combinator to effectively implement recursion in a language that doesn't support recursion directly. However, every language that I'm likely to use already supports recursion so I'm not sure how useful it would be to use the Y-Combinator for that.
Is there a better practical example of Y-Combinator usage that I'm missing? Has anyone actually used one in real production code? Or is using the Y-Combinator really just a mind-bending academic exercise (albeit a pretty cool one).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不同意其他答案:定点(Y)组合器确实有实际应用,但需要非常有想象力的头脑才能找到它们。 就像布鲁斯·麦克亚当。 这是他的论文的摘要 That About Wraps it向上:
这是一篇很棒的论文; 任何对函数式编程感兴趣的人可能都会喜欢阅读它。
I'm going to disagree with other answers: The fixed-point (Y) combinator does have practical applications, but it takes a very imaginative mind to find them. Like Bruce McAdam. Here's the abstract from his paper That About Wraps it Up:
It's a great paper; anyone interested in functional programming will probably enjoy reading it.
您可以查看这篇关于在 C# 中实现 Y Combinator 的精彩文章:递归 Lambda 表达式,它可能会帮助您更好地理解这个想法。
您可能想查看维基百科上的一些不错的文章:定点组合器和
许多函数技术与 Y 组合器相关并且很有用,所以坚持下去! Y 确实很有用,因为它可以让您更好地理解代码,但我认为这对于描述它如何提供帮助并不是特别有帮助。
You could check out this nifty post on implementing the Y Combinator in C#: Recursive Lambda Expressions, it might help you understand the idea better.
You may want to check out some nice articles on Wikipedia: Fixed point combinator and Fixed point theorems
As Nathan says, many functional techniques are related to the Y combinator and are useful, so keep at it! Y really is useful because it lets you understand your code better, but I don't think that's a particularly helpful to describe how it helps.
如果我错了,其他人可以纠正我,但我很确定 Y 组合器是严格学术性的。 想想看:要实现它,您的语言需要支持高阶函数,但不支持递归。 我只知道一种这样的语言:lambda 演算。
因此,在我们的机器从图灵机切换到运行 lambda 演算之前,Y 组合器将是严格学术性的。
注意:与 Y 组合器相关的其他函数技术很有用,所以请坚持下去。 理解 Y 组合器将帮助您理解延续、惰性求值等。
Others can correct me if I'm wrong, but I'm pretty sure the Y combinator is strictly academic. Think about it: to implement it your language needs to support higher-order functions but not recursion. There's only one language I know like that: lambda calculus.
So until our machines switch from Turing machines to running on lambda calculus, the Y combinator will be strictly academic.
Note: other functional techniques related to the Y combinator are useful, so keep at it. Understanding the Y combinator will help you understand continuations, lazy evaluation, etc.
您可以将组合器视为运行函数的虚拟机,您可以通过非递归函数(=高阶函数)来描述该函数。
有时,让这个组合器处于程序控制之下,做类似面向方面编程(记忆、跟踪等)的事情会很好,但我所知道的编程语言都不允许这样做。 大多数时候它可能太麻烦和/或太难学。
You can think of the combinator as the virtual machine which runs your function, which you describe by a non-recursive functional (= higher-order function).
Sometimes it would be nice to have this combinator under program control, to do similar things as aspect oriented programming (memoizing, tracing, ...), but no programming language I know of allows it. Probably it would be too cumbersome most of the time and/or too hard to learn.
下面是我用 F# 制作的小型游戏库的编译器示例。 更具体地说,在上面我需要让 sprites_up 递归地调用自身,否则缩进解析器将无法正常工作。
如果没有 Y Combinator,我就无法正确完成解析器,并且不得不编写如下内容:
这不是一个很好的解决方案,Y Combinator 确实救了我。 但这肯定不是我首先想到的事情。
Here is an example from the compiler for a smallish game library I am making in F#. More specifically, in the above I need to have the sprites_up recursively call itself otherwise the indentation parser would not work correctly.
Without the Y Combinator, I could not have done the parser properly and would have had to resort to writing something like this:
Would not have been a great solution, the Y Combinator really saved me there. It was certainly not the first thing that came to mind though.