Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should functional programming be taught before imperative programming? [closed]

It seems to me that functional programming is a great thing. It eliminates state and makes it much easier to automatically make code run in parallel.

Many programmers who were first taught imperative programming styles find it very difficult to learn functional programming, because it is so different. I began to wonder if programmers who were taught functional programming first would find it hard to begin imperative programming. It seems like it would not be as hard as the other way around, so I thought it would be a good thing if more programmers were taught functional programming first.

So, my question is, should functional programming be taught in school before imperative, and if so, why is it not more common to start with it?

like image 249
Zifre Avatar asked Apr 22 '09 18:04

Zifre


People also ask

Should I learn functional programming first?

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.

What is better imperative or functional programming?

With an imperative approach, a developer writes code that specifies the steps that the computer must take to accomplish the goal. This is sometimes referred to as algorithmic programming. In contrast, a functional approach involves composing the problem as a set of functions to be executed.

When should a functional programming language be used?

Functional Programming is used in situations where we have to perform lots of different operations on the same set of data. Lisp is used for artificial intelligence applications like Machine learning, language processing, Modeling of speech and vision, etc.

Is functional programming faster than imperative?

In contrast due to techniques like pattern matching a whole program like a parser written in an functional language may be much faster than one written in an imperative language because of the possibility of compilers to optimize the code.


2 Answers

Actually, some schools already do it this way around. Where I study (University of Copenhagen), they teach SML in the first semester, as an intro to programming. Then they teach Java afterwards, as an intro to OOP.

I think it works extremely well, and I agree with you it's better than the other way around. Functional programming is fairly intuitive to someone who's not (yet) a programmer. It maps much better to what we were taught as math in high school or earlier, so people who have not yet been exposed to imperative programming usually pick it up without too much trouble.

In fact, there's a trend that people who are new to programming when they enroll pick SML up faster than those who've already learned Java or C++.

It seems like there's a big conceptual jump in going from imperative to functional, but the reverse seems much easier to most. Students generally don't find Java difficult when they're exposed to that after learning SML. Once you know about the "pure" concepts of programming, gluing on side effects is fairly straightforward. But if your entire understanding of programming is based in side effects, it's much harder to imagine that anything is possible without them.

I think a big benefit of this approach is that functional programming principles become an essential part of your programming toolbox, rather than some esoteric add-on you might use if you want to show off. Even when programming in imperative languages, I think there's a benefit in having your background in a functional language. Even when programming in something as low-level as C, there's a benefit to thinking about minimizing state and side effects, and being used to the concept of higher-order functions (even though they're not available in the language)

like image 195
jalf Avatar answered Sep 28 '22 10:09

jalf


Many schools teach functional programming. Some of them even teach it first. I think MIT, for a long time, used to teach scheme in its introduction to computer programing classes.

At my school we covered ML as part of a "comparative programing languages class" that everyone was required to take.

In any case, I don't think functional programming is that difficult to learn for people coming from imperative languages. At least it wasn't for me.

A lot of people think the reason languages like Haskell and Scheme haven't gotten wider adoption is because people are "ruined" from them by imperative programming. That's nonsense.

The real reason those languages haven't seen wide adoption is because they don't use curly braces. Seriously.

The C/Algol syntax style is prevalent because people like the way it looks.

The key to increasing adoption of functional programing is not to talk about how great Haskell is and how evil side effects are, or to say the word "monad" repeatedly. Instead, just create a functional language that uses curly braces and semi-colons. People will use it.

like image 21
Scott Wisniewski Avatar answered Sep 28 '22 10:09

Scott Wisniewski