Functional programming has been “the future” for quite a while now.
IMO the interesting thing about FP is how laziness (enabled by purity) allows the programmer to express as ordinary functions things that would’ve required special syntax or compile-time preprocessing in a strict language. Because of this, many interesting ideas like parser combinators or software transactional memory get discovered first in functional programming and then get translated to other settings. (There’s more where that came from. If you want less familiar examples, see composing financial contracts or computational compactness.)
If you want code that looks like math, I recommend looking at the line of languages that started from Kenneth Iverson’s APL. They don’t look like your typical functional code, but allow very terse expression of algorithms. See K finger exercises for a taste. Yes, these little explosions of line noise are actual working programs, and they really do what they say they do.
Well, you can write a Fibonacci generator in a functional way by closely following the mathematical definition, but it will be very slow. Our compilers/interpreters are not yet that smart.
Fibonacci numbers are a terrible choice of showcase, because they have a closed-form expression (difference of two geometric progressions) computable in logarithmic time (using square-and-multiply), while most recursive solutions require linear time.
One well-known showcase where lazy functional languages clearly win is Hamming numbers. Here’s an LtU discussion where people attempt to translate a ten-line Haskell solution into C++ while keeping the same asymptotic efficiency, with varying results.
Indeed—different problems are best expressed using different styles. I like Scheme for that, it’s easy to choose either a functional or a more imperative approach whenever that makes more sense. Haskell is interesting, but I find it hard to use outside the realm of computer-sciency stuff. I remember the difficulty to get a random number, because getting one has the side-effect of change the entropy state of the universe...
Functional programming has been “the future” for quite a while now.
IMO the interesting thing about FP is how laziness (enabled by purity) allows the programmer to express as ordinary functions things that would’ve required special syntax or compile-time preprocessing in a strict language. Because of this, many interesting ideas like parser combinators or software transactional memory get discovered first in functional programming and then get translated to other settings. (There’s more where that came from. If you want less familiar examples, see composing financial contracts or computational compactness.)
If you want code that looks like math, I recommend looking at the line of languages that started from Kenneth Iverson’s APL. They don’t look like your typical functional code, but allow very terse expression of algorithms. See K finger exercises for a taste. Yes, these little explosions of line noise are actual working programs, and they really do what they say they do.
Well, you can write a Fibonacci generator in a functional way by closely following the mathematical definition, but it will be very slow. Our compilers/interpreters are not yet that smart.
Fibonacci numbers are a terrible choice of showcase, because they have a closed-form expression (difference of two geometric progressions) computable in logarithmic time (using square-and-multiply), while most recursive solutions require linear time.
One well-known showcase where lazy functional languages clearly win is Hamming numbers. Here’s an LtU discussion where people attempt to translate a ten-line Haskell solution into C++ while keeping the same asymptotic efficiency, with varying results.
Indeed—different problems are best expressed using different styles. I like Scheme for that, it’s easy to choose either a functional or a more imperative approach whenever that makes more sense. Haskell is interesting, but I find it hard to use outside the realm of computer-sciency stuff. I remember the difficulty to get a random number, because getting one has the side-effect of change the entropy state of the universe...