13

What are some pros and cons to learning functional programming before other paradigms?

1
  • I first learned how to program in Scheme and right afterwards learned C. The sharpest contrast and lesson was about how shared state makes everything difficult to grasp and debug, and how hard it is to have to learn about recursion right at the beginning - though arguably an essential concept to learn. I feel I am more aware how harmful shared state can be than my colleagues who started with PASCAL or C.
    – Pedro Rolo
    Commented May 6, 2023 at 9:04

7 Answers 7

17

One major problem is that if you start with a language like Haskell everything else will seem just substandard.

Honestly I think starting with a language like Haskell or scheme would be a great idea.

(I admit I am functional language addict) EDIT:

OK what I like about both languages:

Scheme takes a very simple language and build out of it a wonderfully robust language for development. Also SICP is written about scheme which makes it worth learning right there. Scheme is about the simplest thing you could imagine that could be a complete language.

Haskell What is really growing on me is the type system. So many of the bugs I see in other languages are due to the wrong type showing up somewhere. In Haskell that is almost imposible. Also the idea of a lazy language just has some cool things fall out of it. For example you can create infinite data structures in Haskell and then only create the part that you need.

6
  • To be fair, a whole bunch of languages are incorporating more and more functional aspects, so life isn't as bad as it used to be. Commented May 4, 2011 at 7:07
  • True, and for someone who loves FP this is nothing but a good thing
    – Zachary K
    Commented May 4, 2011 at 7:12
  • 3
    I know--I am suggesting this as a good reason for learning FP first. It's a trend that's been making me really happy for a while. Commented May 4, 2011 at 7:16
  • Doesn't really say much to answer the question, though -- why do you like Haskell (or Scheme)? Would they be good as first languages for learning? Why?
    – jimwise
    Commented May 4, 2011 at 17:14
  • Added some comments about both languages
    – Zachary K
    Commented May 5, 2011 at 14:30
6

The biggest plus of learning a functional language before learning an OOP lang is that your programming skills get developed first and then you can easily grasp the OOP concepts. If you direclty start with an OOP language you will have to learn two things simultaenously: "to think about the code" and "to think about the OOD". It can get distracting. First practice with a functional language and develop your programming skills. Then learn OOP and other paradigms. Since OOP was desinged to make up for the shortfalls in structural programming it will be easier to understand why. That is the reason CS courses start with C and then go on to C++.

4
  • 1
    At my university, we first started with a functional language, Standard ML and learned what side-effects are. After that we learned C but we never learned C++.
    – Jonas
    Commented May 4, 2011 at 6:04
  • @Jonas. I guess different countries/universities have very different course structctures. In my university, C followed by C++ is a standard for Comuter Science Engg courses but for other engineering streams, only C is taught.
    – DPD
    Commented May 4, 2011 at 6:29
  • At my university we start with Java, and then just use the language appropriate for the courses after that. For example, C/C++ is used for systems programming, C# for UI, various assembly languages for computer organization and I think whatever you want for games programming Commented May 4, 2011 at 6:56
  • At my university, we started by learning Scheme (with SICP). The first few weeks were entirely functional; OOP was introduced well into the course. I thought it was all laid out very well and rather liked the course. Commented May 4, 2011 at 7:06
6

On the question of how to learn programming by starting with functional programming, two classic recommendations:

  • The first, and obvious one is the classic Structure and Interpretation of Computer Programs, by Abelson and Sussman, which remains one of the best introductions to CS out there, and is taught from a functional perspective, using Scheme. It's available in full online. If you don't start here, you should get here as some point.

  • A more recent text covering most of the same ground at a gentler pace, and with a stronger focus on software engineering is How to Design Programs, by Matthew Felleisen and a bunch of others from the Racket/PLT team, which uses the Racket dialect of Scheme. It's also available online, as is the in-progress second edition. This book has the advantage that it's designed to be used with the DrRacket programming environment, which provides a very friendly interface for beginner and expert alike to experiment with code.

On the question of why start with functional programming, I'd like to point at Bob Harper's Blog. Carnegie Mellon has recently retooled their CS curriculum to teach functional programming first, and Harper has been covering their progress blow-by-blow on his blog. As one of the guys behind the definition of Standard ML, it's obvious that he's for this move, and he argues the reasons for it well.

Finally, I'd caution against learning Haskell first, though others may disagree. While Haskell's pure approach to FP will certainly breed good habits, the language's focus on lazy computation isn't necessarily a good match for the beginner; one of the first, and most important things you will need to learn to do as a programmer is to reason about exactly what your program is doing by looking at the source, and about the relative cost of different approaches to the same problem. I'ts my experience that Haskell's laziness makes both of these activities somewhat of a challenge for even experienced programmers, though Your Mileage May Vary.

5

The main advantage (or, non-disadvantage) of starting out with FP is that most concepts can apply to imperative programming as well. Realm of Racket uses video game analogies to teach both functional and imperative concepts, and dedicated students are left with not only a functional game (n.p.i.), but a solid understanding of conditionals, recursion, loops, ADTs, and event-driven design. These concepts are practically ubiquitous in modern programming and are used constantly.

Even more important, though, is learning how to encode abstractions, something in which FP excels, with the use of higher-order functions and data types. How to Design Programs takes a unique approach to this by teaching through induction. For example, students learn how fold works by looking at the code for taking both the sum and the product of a list, finding what they have in common, and deriving the implementation themselves.

The OOP equivalent of the above would likely involve one or more of the following: interfaces, abstract classes, generics, functors, or (if you're doing it wrong) singletons. While these are perfectly acceptable design patterns in Java, IMHO they do not belong in an introductory curriculum and only serve to obfuscate the underlying principles. Even as someone who was introduced to FP languages "late", I can say that navigating the ever-shifting sea of OOP has been made vastly easier by having a strong functional anchor.

3
  • Almost forgot: if you want to buy Realm of Racket, try this coupon: RACKETEERS. Not sure when it expires, sorry.
    – bug
    Commented Jun 23, 2013 at 2:40
  • +1 for mentioning higher-order functions - I was ready to post my own answer about them. Half my co-workers struggle with the concept, and the rest who do get them don't think of them when it would be the simplest solution.
    – Izkata
    Commented Jun 23, 2013 at 5:43
  • 1
    I hadn't heard of Realm of Racket, but from the title immediately knew what it was, because the title is a reference to it's older brother, Land of Lisp.
    – Magus
    Commented May 20, 2014 at 19:07
4

Functional programming makes things a lot easier. In OOP languages, you have to deal with managing state across multiple threads without ruining that state. In functional languages, when the majority of the work being done is being done by pure functions, you don't have to worry about it.

In terms of speed/performance, I'm not a real performance jockey, but being functional doesn't mean being slow, and the structure of functional languages has little to do with their speed. The syntax of functional languages vary greatly such as the differences between Clojure and Haskell. Clojure is very fast as-is, and can reach (and sometimes exceed) the speeds of Java with after-the-fact optimization.

So it all really depends what you are looking for

2

I think the availability of learning material, some good code samples, and mentors are very important when learning programming languages. Depending on your situation, you may have a mentor that can teach you, etc. but I think functional language resources are very few compared to the mainstream languages. It means you will progress slower compared to learning mainstream languages. But if you are not in a hurry, then this is not a problem.

1
  • 1
    I think your assumptions are wrong. There are good tutorials, books, papers etc. about functional stuff. Moreover, there are lots of papers out there that deal with special things like type system, implementation, virtual machines, lambda calculus and so on.
    – Ingo
    Commented May 4, 2011 at 13:46
1

Perhaps the most important reason to consider learning functional programming languages is the understanding of algebraic data-types. The mental mapping will help in modelling OO class relations and even database design.

The focus on multi-core/multi-processor systems stress the use of parallel algorithms which can be expressed clearer and more concisely in FP. The lambda branch of languages will likely see a strong increase in use in the next one to two decades.

But there are also some common pitfalls. To believe FP is simpler is a big mistake, since calculating the space and time complexity as well as delivering proofs of halting can be much more challenging in lambda calculus, especially in languages which support lazy evaluation.

So, learn both! Or perhaps better: first learn a language which embraces both, such as Scala. If you don't mind tie-die t-shirts and a slight dutch accent, perhaps you'll find the FP lectures by Dr. Erik Meijer useful, which are on MSDN.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.