Scala 是函数式编程语言吗?
我从 Java 学习编程,然后尝试每年学习一种编程语言,第二是 C++,然后是 Python。它来学习下一个,我寻找新的东西,我选择Scala,因为它与Java兼容,并且可以从OOP到函数式编程的一些过渡。
学习新范式、新风格和新思维方式很酷。阅读优雅的 Scala 概念是一次很棒的经历,而且在 Scala 上编码要好得多。
读了很多文章,我遇到了这篇文章批评 Scala:
Scala 不是一种函数式编程语言。它是一种带有闭包的静态类型面向对象语言。
读完这篇文章后,我产生了一些疑问,我真的很喜欢 Scala,并且开始更多地在 Scala 上写作,但是 Scala 适合函数式编程的定义吗?那篇文章说的是真话还是假读者?我必须学习 Haskell 或其他函数式编程语言才能真正体验 FP 吗?
更新:期望有好的例子的理性答案,不会引起争议。
I've learned programming from Java, then tried to learn one programming language per year, second was C++, then Python. It came to learn next one, I looked for something new, I choose Scala because it was compatible with Java and could be some transition from OOP to Functional Programming.
It was cool, learning new paradigms, new style and new way of thinking. It was great experience just read about elegant Scala concepts, and much better to code on Scala.
Reading a lot of articles I faced this article criticizing Scala:
Scala is not a functional programming language. It is a statically typed object oriented language with closures.
After reading this articles some doubts came to me, I really like Scala and was starting to write on Scala more, but is Scala suits definition of Functional Programming? Is that article says truth or just faking readers? Must I learn Haskell or some other Functional Programming Language to really experience FP?
UPDATE: Expecting rational answers with good examples, without causing disputes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Scala 并不强迫您以函数式风格编写。这是完全有效的 Scala:
所以从这个意义上说,可怕的是,Scala 没有功能!大量的副作用、可变状态——在 Java 中能做的一切,在 Scala 中也能做。
尽管如此,Scala 允许您以函数式风格进行编码,并通过多种方式让您的生活变得更轻松(比 Java 更轻松):
这看起来更实用:
值得注意的是,该特定文章的作者似乎对 Scala 的理解很差;几乎每一个在他手中看起来丑陋的例子都是不必要的丑陋。例如,他写道
但如果你注意你正在做的事情,情况并没有那么糟糕:
无论如何,底线是 Scala 不是一种纯函数式编程语言,因此,它的语法并不总是适合函数式编程编程,因为还有其他考虑因素在起作用。然而,它确实具有人们期望从函数式编程语言获得的几乎所有标准功能。
Scala does not force you to write in a functional style. This is perfectly valid Scala:
So in this sense, horrors, Scala is not functional! Side effects galore, mutable state--everything you can do in Java you can do in Scala.
Nonetheless, Scala permits you to code in functional style, and makes your life easier (than in Java) in a number of ways:
This looks somewhat more functional:
It's worth noting that the author of that particular article seems to have a very poor understanding of Scala; pretty much every example that looks ugly in his hands is unnecessarily ugly. For example, he writes
But it's not that bad, if you pay attention to what you're doing:
Anyway, the bottom line is that Scala is not a pure functional programming language, and as such, its syntax is not always ideal for functional programming since there are other considerations at play. It does have virtually all of the standard features that one expects from a functional programming language, however.
我无法说得更好,除了毫无意义的争论之外,这就是所有要说的。
I couldn't say it any better and that's all there is to say except for pointless arguments.
我个人对函数式语言的试金石是教会数字。
方案示例:
(
1+
是一个方案函数,它将 1 添加到其参数中。thrice
接受一个函数 f 并返回一个将 f 与自身组合三次的函数。因此 < code>(thrice 1+) 将 3 添加到其参数中。)(因为
(thrice 1+)
是一个添加 3 的函数,取thrice
给出了一个加九的函数。)我最喜欢的:(
推理留给读者作为练习。最后一个例子是最重要的。)
如果你不能用你的语言写这个例子而不出现可怕的扭曲,那么我说它是不是函数式语言(例如:C/C++)。
如果你可以用你的语言写出这个例子,但看起来很不自然,那么我说你的语言“支持函数式编程”,但并不是真正的函数式语言(例如:Perl)。
如果这个示例可以很好地移植到您的语言,并且实际上看起来与您日常使用它的方式没有太大不同,那么它就是一种函数式语言。
我不知道斯卡拉。有人想告诉我它适合在哪里吗? :-)
My personal litmus test for a functional language is Church numerals.
Scheme example:
(
1+
is a Scheme function that adds 1 to its argument.thrice
takes a function f and returns a function that composes f with itself three times. So(thrice 1+)
adds three to its argument.)(Since
(thrice 1+)
is a function that adds three, taking thethrice
of that gives a function that adds nine.)And my favorite:
(Reasoning left as an exercise for the reader. This last example is the most important.)
If you cannot write this example in your language without horrible contortions, then I say it is not a functional language (example: C/C++).
If you can write this example in your language, but it looks very unnatural, then I say your language "supports functional programming" but is not really a functional language (example: Perl).
If this example ports neatly to your language and actually looks not too different from how you use it day to day, then it's a functional language.
I do not know Scala. Anybody want to tell me where it fits? :-)