Scala 是函数式编程语言吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6166155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:02:51  来源:igfitidea点击:

Is Scala a Functional Programming Language?

javapythonscalahaskellfunctional-programming

提问by Rinat Tainov

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.

我从 Java 学习编程,然后尝试每年学习一种编程语言,其次是 C++,然后是 Python。它来学习下一个,我寻找新的东西,我选择 Scala 因为它与 Java 兼容,并且可能是从 OOP 到函数式编程的一些过渡。

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.

学习新范式、新风格和新思维方式很酷。阅读优雅的 Scala 概念是很棒的体验,而在 Scala 上编码要好得多。

Reading a lot of articles I faced thisarticle criticizing Scala:

读了很多的我面临的文章文章批评斯卡拉:

Scala is not a functional programming language. It is a statically typed object oriented language with closures.

Scala 不是函数式编程语言。它是一种带有闭包的静态类型的面向对象语言。

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?

读完这篇文章后,我产生了一些疑问,我真的很喜欢 Scala 并且开始更多地在 Scala 上写作,但是 Scala 是否适合函数式编程的定义?那篇文章是说实话还是假装读者?我必须学习 Haskell 或其他一些函数式编程语言才能真正体验 FP 吗?

UPDATE: Expecting rational answers with good examples, without causing disputes.

更新:期待理性的回答和好的例子,不会引起争议。

回答by Rex Kerr

Scala does not force you to write in a functional style. This is perfectly valid Scala:

Scala 不会强迫您以函数式风格进行编写。这是完全有效的 Scala:

var i = 1
while (i < 10) {
  println("I like side effects, this is number "+i)
  i += 1
}
case class C(var i: Int, var set: Boolean = false)
def setMe(c: C) = { if (!c.set) { c.set = true; c.i += 1 }; c }
setMe(C(5))

So in this sense, horrors, Scala is notfunctional! Side effects galore, mutable state--everything you can do in Java you can do in Scala.

所以从这个意义上说,可怕的是,Scala不是函数式的!大量的副作用,可变状态——你可以在 Java 中做的一切,你可以在 Scala 中做。

Nonetheless, Scala permitsyou to code in functional style, and makes your life easier (than in Java) in a number of ways:

尽管如此,Scala允许您以函数式风格进行编码,并以多种方式让您的生活更轻松(比在 Java 中更轻松):

  • There are first-class functions
  • There is an immutable collections library
  • Tail recursion is supported (to the extent that the JVM can manage)
  • Pattern matching is supported
  • (etc.)
  • 有一流的功能
  • 有一个不可变的集合库
  • 支持尾递归(在JVM可以管理的范围内)
  • 支持模式匹配
  • (等等。)

This looks somewhat more functional:

这看起来更实用:

for (i <- 1 to 10) println("Sometimes side effects are a necessary evil; this is number"+i)
case class C(i: Int, set: Boolean = false)
def setIt(c: C, f: Int=>Int) = C(f(c.i), true)
setIt(C(5), _+1)

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

值得注意的是,那篇特定文章的作者似乎对 Scala 的理解很差;几乎每个在他手中看起来丑陋的例子都是不必要的丑陋。例如,他写

def x(a: Int, b: Int) = a + b
def y = Function.curried(x _)(1)

But it's not that bad, if you pay attention to what you're doing:

但这并没有那么糟糕,如果你注意你在做什么:

def x(a: Int)(b: Int) = a + b
val y = x(1) _

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.

无论如何,最重要的是 Scala 不是一种纯粹的函数式编程语言,因此,它的语法并不总是适合函数式编程,因为还有其他考虑因素在起作用。然而,它确实具有人们期望从函数式编程语言中获得的几乎所有标准特性。

回答by Nemo

My personal litmus test for a functional language is Church numerals.

我个人对功能语言的试金石是教堂数字。

Scheme example:

方案示例:

(define (thrice f)
    (lambda (x)
        (f (f (f x))))))

((thrice 1+) 0)
  => 3

(1+is a Scheme function that adds 1 to its argument. thricetakes a function f and returns a function that composes f with itself three times. So (thrice 1+)adds three to its argument.)

(1+是一个将 1 添加到其参数的 Scheme 函数。 thrice接受一个函数 f 并返回一个将 f 与自身组合三次的函数。因此(thrice 1+)将其添加到其参数中。)

((thrice (thrice 1+)) 0)
  => 9

(Since (thrice 1+)is a function that adds three, taking the thriceof that gives a function that adds nine.)

(因为(thrice 1+)是一个加三的函数,取 的thrice给出一个加九的函数。)

And my favorite:

还有我最喜欢的:

(((thrice thrice) 1+) 0)
  => 27

(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++).

如果你不能用你的语言编写这个例子而没有可怕的扭曲,那么我说它不是一种函数式语言(例如: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).

如果你可以用你的语言编写这个例子,但它看起来很不自然,那么我说你的语言“支持函数式编程”但并不是真正的函数式语言(例如: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? :-)

我不知道斯卡拉。有没有人想告诉我哪里合适?:-)

回答by Kim Stebel

Scala is a multi-paradigm programming language designed to integrate features of object-oriented programming and functional programming.

Scala 是一种多范式编程语言,旨在集成面向对象编程和函数式编程的特性。

I couldn't say it any better and that's all there is to say except for pointless arguments.

我说得再好不过了,除了毫无意义的争论之外,我要说的就是这些。