views:

1360

answers:

15

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?

+4  A: 

It could possibly be a mindshare issue, as most teachers/professors probably learned imperative styles first as well.

Also, I would guess that there is far larger body of work available for teaching imperitive styles.

chills42
+2  A: 

I can only assume its the fact that OOP seemed to be a favorite buzzword / style, So Schools stuck to that,

I was taught OOP design from the get go, its only recently ive been teaching myself the Functional style of programming and i can see it has its advantages.

Fusspawn
+2  A: 

Edit: What follows reflects the original title, "Why is functional programming not taught in schools," Schools have teachers, not professors. Schoolteachers do not write their own textbooks.

The textbook companies from which the teachers are allowed to purchase their materials are the largest problem. The textbook companies are quick to jump on "the next big thing", which was OOP a few years ago. Functional programming has fallen by the wayside. Many teachers cannot or are not allowed to teach a course without a textbook, so the course selection generally follows the availability of textbooks from the big vendors.

eleven81
I find this very hard to believe. Also very subjective. Additionally, Functional programming languages are covered in any programming language course.
kgrad
"Coursepacks" are basically textbooks written by the professor, which would make that last point moot. Well, as long as a professor actually writes it.
nilamo
There is now an excellent textbook for schoolteachers: http://htdp.org
Norman Ramsey
A: 

First, I find the basis of your question faulty because I was taught functional programming at school. (It wasn't what I started with, but we did do some functional programming.)

Second, I question the ease of transferring from functional coding to imperative coding. I don't think it'd be as easy as you make it out to be.

Third, programming is (for most people) a job skill. The vast majority of shops use imperative programming. Therefore, it's most useful to future coders to learn imperative programming.

Daniel Lew
+9  A: 

Many (most ?) schools teach functionnal programming. Very few teach it first tough, for many reasons.

  • Most development environments for functionnal languages suck, and require extensive programming knowledge to use adequately. This is becoming less and less true, but we're still far away from Visual Studio for Haskell.

  • It's harder to jump in the 'flashy stuff'. GUI toolkits and libs suck for most functionnal languages. Showing stuff on-screen and rewarding the student is important.

  • Self-taught programmers tend to gravitate to imperative/OO languages for historical reasons. Availability of BASIC in their youth, knowing that their favorite game was written in C or C++, you name it.

  • Simple ressources and tutorials for functional programming languages are harder to come by. Compare the number of C# samples to Lisp samples on Code Project. Keep in mind that Lisp is over 5 times as old.

jfclavette
I would also add that the complexity of the functional style of programming and concepts are harder to grasp than object oriented which seem to "make sense". Also, the prevalence of OOP in the workforce is much higher than FP and thus a random person is more likely to use an OO language than a functional one.
kgrad
Dr. Scheme gives the lie to your first point. It's a Scheme IDE aimed at CS students.
RossFabricant
Yeah, I had Dr. Scheme in mind when I mitigated the first point with 'Most' and 'less and less true'. Still, Dr. Scheme, while good, isn't exactly wonderful when it comes to debugging facilities. But I admit that to a total beginner, those might not be very useful. :)
jfclavette
@kgrad: I disagree. FP is no harder to grasp than OOP, if you're starting out. Once you're deeply entrenched in the imperative-programming mindset, FP becomes much much harder to learn.
jalf
@jaff: I disagree with you, I think iteration and mutability are easier concepts to grasp than recursion and immutability. I obviously have no stats on this and as such it is my personal opinion.
kgrad
I highly disagree with the "rewarding the student" part. When I was in university, very few assignments had GUIs, and that was only 5 years ago when I graduated. I really hope things haven't changed that much.
Kibbee
Mathematics are immutable. If x=5 in an equation, then x keeps that value. You can't assign a new value to it. Students are used to that. Anyway, my point isn't that FP is *easier*, just that it's no *harder* to someone starting from scratch. Like I said in my answer, I've seen a lot of people start out learning FP, and it's just not a problem. They don't struggle with it, the way people familiar with imperative programming do. They have no expectation that variables should be mutable.
jalf
I don't think the 'flashy stuff' as you put it, has something to do in an introductory CS/programming class. It's more about concepts.
boutta
To your second point: I think the REPL gives instant feedback and experience of success (albeit not graphical). It takes much more time and effort to write a complete program that shows some "flashy stuff" "on-screen".
thSoft
A: 

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?

Though I was not fortunate enough to start off with a functional language, I do have colleagues who did. I also had a friend who studied Math, and the only language he learned while in school was Haskell!

There really are two things: Computer Science and Computer Engineering. Though the line is very thin, IMO, it depends a lot on the course (as mentioned above) and the concerned department's focus area which language to cut your teeth with. Most engineering schools start with one of C, C++ or Java -- which have a potentially large job market. Others, may start with Lisp, Haskell etc.

dirkgently
A: 

Functional programming is taught in most universities. Part of the reason it's not taught in high school is probably due to the "Functional programming is to hard" myth.

Rayne
Did someone just do a hit-and-downvote?
Rayne
A: 

Dunno about your school by in my undergrad studies ML was an essential part of the basic programming languages course and there was an extra curriculum course dedicated to Haskel.

shoosh
+19  A: 

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)

jalf
My own experience as a student was Scheme first, then C, then OCaml (compiler course) and not so good OO in Java. Scheme was great, as complete beginners we got to do fun stuff like an inference system or symbolic calculus.I've been teaching C as a beginner language (I don't control that choice, yet) and state IS a difficulty for a number of students. But the biggest problem is conceptual noise. C is OK-ish but hard, Java is worse. Minimal syntax and semantics like Scheme or Smalltalk work really well for introductory courses. For advanced OO I'd pick Smalltalk or Eiffel over anything else.
Damien Pollet
+8  A: 

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.

Scott Wisniewski
When you are right, you are right.
Prankster
This made me laugh, but I think you're right.
Zifre
Python is quite popular and doesn't use curly braces and semi-colons.
Christian
I found Scheme simply hard. It was frustrating after using Java API - I want to construct software, not implement classes in terms of lists damn it! The biggest problem with functional languages is that they are write-only, and without extensive comments it is hard to pick up and start using somebody else's code just like that. Clojure holds some promise, however.
Hamish Grubijan
+1  A: 

The clear control flow of imperative programming lends itself well to implementation and analysis of algorithms in a teaching environment. Object-oriented programming is a convenient extension of that, so it is naturally what gets used most often. Functional programming (declarative programming of any kind really), on the other hand, is a completely separate paradigm that requires a whole new set of considerations (performance and otherwise) many of which are much easier to visualize if you understand imperative programming first. After all, it all boils down to an imperative language in the end.

Volte
+1 for it boils down to an imperative language - assembly is imperative, C is imperative, and most compilers emit either assembly or C.
Hamish Grubijan
+1  A: 

There has to be an historical angle to this (showing my age) for the curriculums in school. Functional was all I had to learn when I started.

But putting that aside, you have to start somewhere so one of them has to be first. If you start with imperative then there will be things you reach for that wont be there when you learn functional and you'll have to get used to doing things differently. If you start with functional and move onto imperative then you'll have to get used to learning new concepts/constructs and remembering that they are there it use.

Whenever you are programming you are trying to solve a problem. It is good to have both in your toolbox to reach for to solve the problem at hand. That's why I think it would be best to start with imerative and then learn functional: if you find yourself reaching for something not there that's indicative of having chosen the wrong tool to solve your problem.

Other than that methinks it's a toss up.

yetanotherdave
A: 

Honestly, I think this is the wrong question to ask.

If the goal of an introductory class is to teach basic computer science, algorithms, programmatically solving problems, then your course is fundamental and you can choose one based on clarity, simplicity, and getting your ideas across. Some functional languages will be great for this. Particularly if the students have little or no previous experience.

Usually this isn't really the goal of many introductory classes, though. They may require certain techniques or certain languages for a later course. They may want to leverage particular libraries, etc. They may just have to match someone elses checkbox list of "features".

So I think you want to turn your question on its head. Figure out what it is the course is actually trying to achieve, and pick the best language you can for that purpose. In some cases, this may be a functional language.

If what you are really asking is how best to teach introductory level programming and computer science, that's a different can of worms.

simon
+1  A: 

I was actually just at a talk given by the person who's developing Bootstrap (a programming curriculum currently run by Citizen Schools). He seemed to think the functional programming style provided a better background for algebra, since it hits on the concept of functions as both processes and objects (with properties of their own). (Of course, declarative programming languages can have first-class functions, too, but the focus is not on that nearly as much.)

Personally, I think that teaching functional programming first is worthwhile. The declarative approach is taught very early on in math class, so functional programming provides some new concepts that declarative programming does not. I agree with many of the above posters that the bit about it being "too hard" is a myth, it's been done.

L33tminion
A: 

I think, it should, yes.

Also - no console applications and no "color palette mixers", but unit tests first.

Only if the students behave well and write test cases with extremely high coverage, they MAY be allowed to have a quick look at how to read characters from keyboard.

In my opinion, most bad coding practices (code smells, antipatterns) come from a total ignorance towards the theory and best practices.

So - teach them F# first, forbid the use of console i / o and GUIs and only use unit tests.

Second, teach them imperative / functional mixed style in an OOP context with full use and explanation of design patterns.

Then - and ONLY then they can try to put clickybuttons somewhere, maybe as a reward. But theory and good coding practices first. Otherwise we create the next generation of "RAD" - victims that can't even use indentation correctly and mistaken ctrl+c / ctrl+v for software development.

stormianrootsolver
we are a MSFT shop, but not one person in my company uses F# or PowerShell. Why push crap on people?
Hamish Grubijan