Scala 是函数式编程语言吗?

发布于 2024-11-10 10:57:43 字数 556 浏览 1 评论 0原文

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

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

发布评论

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

评论(3

三岁铭 2024-11-17 10:57:45

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))

所以从这个意义上说,可怕的是,Scala 没有功能!大量的副作用、可变状态——在 Java 中能做的一切,在 Scala 中也能做。

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

  • 有一流的函数
  • 有一个不可变的集合库
  • 支持尾递归(在 JVM 可以管理的范围内)
  • 支持模式匹配
  • (等等)

这看起来更实用:

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)

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

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

但如果你注意你正在做的事情,情况并没有那么糟糕:

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

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

Scala does not force you to write in a functional style. This is perfectly valid 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 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:

  • 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.)

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

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.

巾帼英雄 2024-11-17 10:57:45

Scala 是一种多范式编程
旨在集成的语言
面向对象的特点
编程和功能
编程。

我无法说得更好,除了毫无意义的争论之外,这就是所有要说的。

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

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

贩梦商人 2024-11-17 10:57:45

我个人对函数式语言的试金石是教会数字。

方案示例:

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

((thrice 1+) 0)
  => 3

(1+ 是一个方案函数,它将 1 添加到其参数中。thrice 接受一个函数 f 并返回一个将 f 与自身组合三次的函数。因此 < code>(thrice 1+) 将 3 添加到其参数中。)

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

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

我最喜欢的:(

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

推理留给读者作为练习。最后一个例子是最重要的。)

如果你不能用你的语言写这个例子而不出现可怕的扭曲,那么我说它是不是函数式语言(例如:C/C++)。

如果你可以用你的语言写出这个例子,但看起来很不自然,那么我说你的语言“支持函数式编程”,但并不是真正的函数式语言(例如:Perl)。

如果这个示例可以很好地移植到您的语言,并且实际上看起来与您日常使用它的方式没有太大不同,那么它就是一种函数式语言。

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

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. thrice takes a function f and returns a function that composes f with itself three times. So (thrice 1+) adds three to its argument.)

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

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

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

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? :-)

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