gpt4 book ai didi

functional-programming - 函数式编程类(class)结束时的 2 个问题

转载 作者:行者123 更新时间:2023-12-04 08:43:08 25 4
gpt4 key购买 nike

这似乎是我可以从我刚刚完成的如何设计程序(简化 Racket )类(class)中直接从类(class)的讲义中学到的两件事:

1) 尾调用优化,以及在非函数式语言中的缺失:

Sadly, most other languages do not support TAIL CALL OPTIMIZATION. Put another way, they do build up a stack even for tail calls.

Tail call optimization was invented in the mid 70s, long after the main elements of most languages were developed. Because they do not have tail call optimization, these languages provide a fixed set of LOOPING CONSTRUCTS that make it possible to traverse arbitrary sized data.



a) 在没有它的过程语言中,这种优化的等价物是什么?
b) 使用这些等价物是否意味着我们避免在类似情况下用没有它的语言构建堆栈?

2)变异和多核处理器

This mechanism is fundamental in almost any other language you program in. We have delayed introducing it until now for several reasons:

  • despite being fundamental, it is surprisingly complex

  • overuse of it leads to programs that are not amenable to parallelization (running on multiple processors). Since multi-core computers are now common, the ability to use mutation only when needed is becoming more and more important

  • overuse of mutation can also make it difficult to understand programs, and difficult to test them well

But mutable variables are important, and learning this mechanism will give you more preparation to work with Java, Python and many other languages. Even in such languages, you want to use a style called "mostly functional programming".



在参加这门类(class)之前,我学习了一些 Java、Python 和 C++,所以我认为变异是理所当然的。现在这一切都被上述声明抛在了脑后。我的问题是:

a) 我在哪里可以找到有关第 2 个项目符号中建议的内容以及如何处理的更多详细信息,以及
b) 什么样的模式会从“主要是函数式编程”的风格中出现,而不是一种更粗心的风格,如果我继续使用其他语言而不是学习这门类(class),我可能会出现这种风格?

最佳答案

正如 Leppie 指出的那样,对于它们支持的特定类型的循环,循环结构设法恢复正确尾调用的空间节省。循环结构的唯一问题是你所拥有的结构永远不够,除非你只是将球扔进用户的 field 并强制他们明确地对堆栈进行建模。

举个例子,假设您正在使用循环遍历二叉树。它有效……但是您需要明确跟踪“要返回的对象”。尾部调用语言中的递归遍历允许您在不需要时不浪费空间,并且不强制您自己跟踪堆栈,从而让您拥有蛋糕并吃掉它。

您关于并行性和并发性的问题更加开放,最好的指针可能指向研究领域,而不是现有解决方案。我认为大多数人都会同意计算世界正在发生危机。我们如何使我们的大量突变编程技能适应新的多核世界?

简单地切换到功能范式在这里也不是 Elixir ;我们仍然不知道如何编写高级代码并生成极快的非变异运行代码。不过,很多人都在为此努力!

关于functional-programming - 函数式编程类(class)结束时的 2 个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8517004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com