score:39

Accepted answer

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.

score:19

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

score:21

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.


Related Query

More Query from same tag