21

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.

Rinat Tainov
  • 1,479
  • 2
  • 19
  • 39
  • 6
    Please remove all personal opinions not related to the question. It wastes our valuable time which should be invested in solving your problem. – the_drow May 29 '11 at 05:45
  • 1
    Related: http://stackoverflow.com/questions/3962604/is-javascript-a-functional-programming-language/3962690#3962690 and http://stackoverflow.com/questions/3527753/why-is-smalltalk-not-a-functional-programming-language/3528329#3528329. – missingfaktor May 29 '11 at 05:49
  • 4
    "Object-oriented languages encourage the overuse of state and inheritance." "Functional languages encourage the overuse of higher-order programming." (from Peter van Roy och Seif Haridi. Copyright © 2004. 0-262-22069-5. The MIT Press. Concepts, Techniques, and Models of Computer Programming.). – oluies May 29 '11 at 07:24
  • 1
    The question is impossible to answer as there is no clear definition of what a functional language is. You can certainly program in a functional style in Scala. Does that make it functional? – augustss May 29 '11 at 10:00

3 Answers3

39

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.

Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
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.

Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
Kim Stebel
  • 41,826
  • 12
  • 125
  • 142
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? :-)

Nemo
  • 70,042
  • 10
  • 116
  • 153
  • 9
    `def thrice[A](f: A => A) = f andThen f andThen f` `thrice[Int](1+)(0) // gives 3` `thrice(thrice[Int](1+))(0) // gives 9` `thrice(thrice[Int])(1+)(0) // gives 27`. Howzat? – Rex Kerr May 29 '11 at 06:08
  • 2
    (FWIW, it looks unnatural, but that's because it's a horribly inefficient way to create the number 27, and because applying the successor operation "1+" is not how most of us think of doing arithmetic. (Nor, for that matter, is thricing a function all that useful, since there's `Iterator.iterate` to apply functions arbitrarily deep.) – Rex Kerr May 29 '11 at 06:21
  • 1
    @Rex: Well, you wouldn't add 27 in Scheme like this, either, but that's not the point. This is actually how Alonzo Church represented natural numbers in the original lambda calculus. (When all you have is `lambda`, your options are limited.) What I call "thrice", he called "3"; as in the actual integer 3. The ability to write a "thrice" that can be applied to itself is a pretty good test of how "functional" a language is, in my view. – Nemo May 29 '11 at 06:26
  • I don't feel it unnatural in perl: `perl -le 'sub Inc{1+shift} sub Thrice {my $f=shift; sub {$f->($f->($f->(shift)))}} print for Thrice(\&Inc)->(0), Thrice(Thrice(\&Inc))->(0), Thrice(\&Thrice)->(\&Inc)->(0)'` – Hynek -Pichi- Vychodil May 29 '11 at 11:43
  • 1
    @Hyenk: Well, I suppose line noise would look "natural" to most Perl programmers :-) – Nemo May 29 '11 at 16:26
  • 1
    @Nemo: Beauty is in the eye of the beholder. I can tell same about Scheme. `Thrice(\&Thrice)->(\&Inc)->(0)` seems for me better understandable than `(((thrice thrice) 1+) 0)`, its more explicit what is going on. – Hynek -Pichi- Vychodil May 29 '11 at 21:29
  • A few other languages: https://gist.github.com/2790981 – Mauricio Scheffer May 28 '12 at 22:22
  • honestly, looking at the gist i cannot say it feels particularly awkward in any of the languages. Would like to see a C version – lud Sep 06 '13 at 11:58
  • 1
    At least the Scala function can be written on one line and still be readable to an extent. The others take a lot of hoop-jumping to look clear. Perl's comment just looks like someone vomited onto their keyboard. :/ – RustyFluff Jan 11 '21 at 20:50