Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Scala a Functional Programming Language? [closed]

Tags:

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.

like image 399
Rinat Tainov Avatar asked May 29 '11 05:05

Rinat Tainov


People also ask

Is Scala a functional programming language?

Scala is functionalScala is also a functional language in the sense that every function is a value. Scala provides a lightweight syntax for defining anonymous functions, it supports higher-order functions, it allows functions to be nested, and it supports currying.

Is Scala open source?

The Scala programming language is an open source project with a very diverse community, where people from all over the world contribute their work, with everyone benefiting from friendly help and advice, and kindly helping others in return.

Is Scala still used?

Scala TodayScala is actually still used very often, and it likely will not suddenly disappear anytime soon. That being said, those picking up Scala for Data Science in particular might want to also learn another language as well.

Is Scala good for functional programming?

Scala is a language that features full support of Functional Programming as well as the Object-Oriented Paradigm. It is one of the most widely utilized languages by the Functional community, up to par with F#, and Haskell, Clojure, among others.


3 Answers

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.

like image 126
Rex Kerr Avatar answered Sep 22 '22 17:09

Rex Kerr


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.

like image 21
Kim Stebel Avatar answered Sep 24 '22 17:09

Kim Stebel


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

like image 19
Nemo Avatar answered Sep 20 '22 17:09

Nemo