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...
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...