问题
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?
回答1:
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)
回答2:
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.
回答3:
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 resources 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.
回答4:
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.
回答5:
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.
回答6:
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.
回答7:
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.
回答8:
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.
回答9:
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.
回答10:
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.
回答11:
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.
回答12:
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.
回答13:
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.
回答14:
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.
回答15:
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.
来源:https://stackoverflow.com/questions/778461/should-functional-programming-be-taught-before-imperative-programming