开发者

Is Scala a Functional Programming Language? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

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 t开发者_运维知识库o 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.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜