How to get started with functional programming
johndcook.com
johndcook.com
Trying to write pure functions in a standard language is completely the wrong approach. You might as well say, tie one hand behind your back and try to write with the other hand.
Many functional languages don't require pure functions.
Many functional languages have garbage collectors, and they are fundamental for writing pure functional data structures. (It's almost impossible to properly emulate this in, say C, especially if your structures have cycles)
Many functional languages have other great features like type inference, macros, data patterns and more. Just using an existing language won't give you any taste for these.
Yes. Though I would suggest a newcomer to pick a language that requires purity, in order to make the transition cleaner.
One of the most notable one is:
http://www.cs.utexas.edu/~shmat/courses/cs345/whyfp.pdf
Benefits include: Easier reasoning about code correctness. More powerful abstractions leading to clearer and more succint code. Easier mechanical refactoring of code. Easier parallelism. More powerful compiler optimizations.
Apart from any benefit from actually using any language or tool, there's also the joy of learning.
Its better to start with a language that helps you do the right thing. Additionally if you do FP in Java or C++ you will not get all the benefits and after you tried it conclude that FP is not worth it.
Start with Haskell, ML, Clojure or something like that.
Just make it Haskell. If you want to learn FP, you cannot do better than Haskell. Of course if you want to write apps tomorrow Clojure is a pretty good choice.
I recommend Haskell over OCaml for learning. Haskell has a cleaner syntax, a living community, and emphasises purity.
So, are you arguing that good java devs don't follow the standart java comunity idioms?
Sure there are some devs that to that but to say FP is natural to any good Java dev is a bit of a stratch.
Pure functional programming is a different enough approach that it requires a whole different eco-system of libraries. Differently-optimizing compilers, to avoid large runtime penalties. And a different mindset.
I think the author is talking about acquiring the different mindset, but in a setting where it is just not very practical.
Just start doing it, do some research for stuff you don't understand, and see if it flies for you.
I'm not an elitist although I might very well come out as one but any article with a title analogous to what's above is by definition missing the target. My reasoning goes that anyone who's worth his salt and interested in functional programming has got, or will get, started with it without any such an article. Anyone who's either not worth his salt or not interested in it will not start functional programming or programming or anything because of some article (or a few of them). In general, of course.
In a language like c++, what would be an elegant naming convention to differentiate between pure functions and others?
His criticism is pretty fair, too - far more substance than a dozen "oh wow, look at how small quicksort / fibonacci is" blog posts.
[0] http://www.st.cs.uni-saarland.de/edu/seminare/2005/advanced-...
Interestingly, you can also say that Cobol has been proven to work for business applications over and over. And people build successful websites with PHP.
- Jax and Daxter (and Crash Bandicoot): http://ynniv.com/blog/2005/12/lisp-in-games-naughty-dogs-jax... - Abuse: http://en.wikipedia.org/wiki/Abuse_(computer_game) - Lua (even though it is not strictly functional) is used quite a bit throughout the game industry.
A single programmer (who could easily be one of the top ten Lisp programmers in the world) wrote GOAL. While he called his Lisp techniques and programming practices "revolutionary," others referred to them as "code encryption," since only he could understand them.
So we found one example, and it didn't prove FP is beneficial to productivity.
D is a bit like C++. And its type system supports pure functions. I don't know if there's a standard naming convention, but D is worth a look.
I believe that you can write a totally immutable C++ program. But it's probably much harder than writing the same program in Haskell. You could try writing an immutable program in D. D is somewhat similar to C++, but much saner.
This means immutable data structures, writing for composability, side effect free methods, list processing with "closures" instead of for loops etc.
Idiomatic Java is object-oriented, and OO is inherently stateful: side-effects are everywhere and are totally acceptable. Also, it tends to use iteration rather than recursion, doesn't use higher-order functions, classes and methods are designed for "inheritability" rather than composability. However, I'd love to be proved wrong. Care to show any open-source Java code that uses FP techniques?
Objects have state, as have immutable data structures.
"doesn't use higher-order functions,"
But they use higher-order objects.
"classes and methods are designed for "inheritability"
No, at least for 10 years it has been debunked in Java that inheritance is a good thing. Inheritance outside of some small domains like GUI libraries is a bad thing as inheritance is too rigid. Most bad Java developers use inheritance because they've been teached OO in a completly wrong way with dogs and cats or cars and trucks.
I think the common understanding for years in Java/C# etc. is that composition is much better than inheritance.
This article from 1998 describes the problem of inheritance vs. composition in java
http://www.artima.com/designtechniques/compoinhP.html
"Care to show any open-source Java code that uses FP techniques?"
No, because to be frank, I have not much knowledge about Java open source. And the projects I've looked into (Apache comes to mind) have sub-industry quality or are outright awful. Not sure why this is the case though.