Take the 2-minute tour ×

I'm starting to learn Function Programming, and I really wanna learn Scala, not Haskell or Lisp.

But some people claim that learning Scala as the first functional language slows down your learning of Functional Programming, because Scala allows you to program both ways, and one tends to program the procedural way when confronted with a hard problem.

My question is, how can i make sure that i'm programming in a purely functional way?

(I mean, maybe i'm not able to distinguish exactly both styles, and i inadvertedly program procedurally).

I know, for example, that I should only use vals and not vars.

share|improve this question
Not related to the question, but if you want some good books, read in this specific order: amazon.com/Programming-Scala-Comprehensive-Step-Step/dp/…manning.com/suerethmanning.com/bjarnason –  folone May 7 '13 at 13:08
Oh, and there's wart remover by Brian McKenna: github.com/pufuwozu/wartremover –  folone May 7 '13 at 13:25
"Scala is a gateway drug to Haskell" stackoverflow.com/a/7594599/367649 –  Marimuthu Madasamy May 7 '13 at 13:38
Purchase this book manning.com/bjarnason –  oluies May 8 '13 at 8:39

5 Answers 5

up vote 10 down vote accepted

The other answers have made some good points, but for an attempt to quickly get down some guidelines, here's how I'd start:

Firstly, some things to completely avoid:

  • Don't use the var keyword.
  • Don't use the while keyword.
  • Don't use anything in the scala.collection.mutable package.
  • Don't use the asInstanceOf method.
  • Don't use null. If you ever come across null (in someone else's code), immediately wrap it in a more appropriate datatype (usually Option will do nicely).

Then, a couple of things to generally avoid:

  • Be wary of calling anything with a return type of Unit. A function with a return type of Unit is either doing nothing, or acting only by side-effects. In some cases you won't be able to avoid this (IO being the obvious one), but where you see it elsewhere it's probably a sign of impurity.
  • Be wary of calling into Java libraries - they are typically not designed with functional programming in mind, and will often require you to abandon the functional approach.

Once you've avoided these things, what can you do to move your code to being more functional?

  • When you're performing direct recursion, look for opportunities to generalise it through the use of higher order combinators. fold is likely your biggest candidate here - most operations on lists can be implemented in terms of a suitable fold.
  • When you see destructuring operations on a data structure (typically through pattern matching), consider whether instead you can lift the computation into the structure and avoid destructuring it. An obvious example is the following code snippet:

    foo match {
      case Some(x) => x + 2
      case None => None

    can be replaced with:

    foo map ( _ + 2 )
share|improve this answer
Wrapping null to a Option may lead to a cargo cult. I don't think you have to follow this rule blindly: sometimes code is slightly messed up with the use of local Options, where if use would be enough. –  om-nom-nom May 15 '13 at 0:29
It's a fair point. I wouldn't necessarily recommend it in general, but for the author's specific case - making certain that they're writing in a purely functional way - I'd say it's a reasonable approach to just remove null entirely from the things they have to deal with. –  Impredicative May 16 '13 at 12:30

I dare say that your goal is already misleading:


If you are really interested in learning concepts of function programming, then why not use a language such as Haskell that (more or less) does not allow you to use procedural or object-oriented concepts? In the end, the language is "just" a tool that helps you learning concepts of FP, you could just as well read loads of papers about FP. At least theoretically, I think it is usually easier to learn concepts of computer science with concrete tools at hand.

On the other hand, if you are interested in learning the language Scala, then why not use all features that it offers, regardless of whether they stem from the FP or the OO world?

In order to conclude with a somewhat practical advise: You could search for FP tutorials that use Scala or for blog entries etc. that describe how to realise certain FP-concepts in Scala and try to follow them. This way, it is less likely that you make use of non-FP concepts.

share|improve this answer

You don't buy a Ferarri to deliver furniture. Scala's fundamental strength is the fact that in your words, it goes both ways:). Whether or not you are programming in a functional style is decided by the techniques you use.

The best thing you can do is thoroughly review fundamental concepts of functional programming and seek the appropriate Scala implementation of the respective concepts. But if you want to program purely functional style, then go for Haskell, Lisp, Erlang, OCaml, or whatever other purely functional dialect.

Functional programming


Functional thinking


If you want to learn Scala, then make sure to include both OO and FP in your learning curve. Lambda expressions + OO concepts + syntactic sugar made possible by IMHO the most advanced compiler on the face of the planet lead to something quite amazing. Take advantage of it!

share|improve this answer
I would say that most Lisps and Erlang are not pure functional. –  om-nom-nom May 15 '13 at 0:20

I think learning is non linear process, it helps to see lots of ways of doing the same thing, also be opportunistic and use any learning resources that are available for you. For example Martin Odersky the creator of Scala offers a free course called "Functional Programming Principles in Scala" https://class.coursera.org/progfun-002/class/index there are some very high quality video lectures, and some really good assignments where the automated grader will tell you that your code is not functional enough and you loose style points because you are using var instead of val

I think the thing you want to focus on is learning the Functional Programming Paradigm and for me learning a paradigm is about learning what types of problems are easy to solve in one paradigm and are hard to solve in another paradigm. Focus on the paradigm and I think you will find that learning both about Haskell and Scala will teach you the functional paradigm faster, because you will be able to ask the question what are the common features between Scala and Haskell, what are the differences .... etc

share|improve this answer


That's already a good start, other non-so-functional things to avoid are mutable collections and loops.

Have a look at immutable collections and recursion instead.

Of course, once you are familiar with the functional concepts, there might also be good reasons to use scala's non-functional features.

share|improve this answer

Your Answer


