Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I, for one, prefer recursion to explicit iteration in most cases. Anyway, if you were going to write the answer in Haskell, it's a three-liner and it's recursive.


Why do you need so many lines?

    fib = 1 : zipWith (+) (0 : fib) fib


Because the three liner is easier to read


Btw, which three-liner do you mean?


fib 0 = 0

fib 1 = 1

fib n = fib (n-1) + fib (n-2)


Oh, that version takes exponential time to compute. The one-liner I gave computes up to the n-th Fibonacci number in linear time.


Your version is definitely more performant, but the three liner is just a bit nicer to read (especially for beginners) :)


Yes. It depends on what you want to show. Your version shows how to define functions and some simple pattern matching.


Having worked with Haskell for a year now I still can't read much more than functions and pattern matching! :D


Not in Haskell, it doesn't. Haskell stores everything for you, right?


No, it doesn't.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: