Re:On the Linear Time Algorithm For Finding Fibonacci Numbers
by cperciva on Tuesday July 07, 2009 @11:21AM
(#691613)
It works in non-lazy languages, too... it just happens to be a waste of cycles. :-)
Re:On the Linear Time Algorithm For Finding Fibonacci Numbers
by jcl on Tuesday July 07, 2009 @11:23AM
(#691619)
How? Giving f(N) in terms of f(N) hardly seems useful, in any language. (It's hard to tell if you're joking without a smiley.)
Re:On the Linear Time Algorithm For Finding Fibonacci Numbers
by eru on Wednesday July 08, 2009 @08:34AM
(#693765)
I am too dumb / lazy to think of an example for the fibonaccy numbers at the top of my head. But suppose your problem was giving a list of natural numbers, then giving a circular definition like
> naturals = 0 : (map (+1) naturals)
actually works in Haskell.
(In Haskell a:b means the same as (cons a b) in Lisp. I also added parens to make the definition more readable for non-Haskellers.)
Re:On the Linear Time Algorithm For Finding Fibonacci Numbers
by jcl on Wednesday July 08, 2009 @04:14PM
(#694581)
So, that actually works by establishing a base value and a derivation step... i.e., it defines N(0), then it expresses N(n) in terms of N(n - 1).
If you try to do the same thing for Fibonacci numbers, you would need to express F(n) in terms of earlier values, not in terms of F(n). What cperciva is jokingly proposing is the equivalent of:
naturals = naturals
which is hardly helpful for computing the values. Lazy languages would not help you there.
That might just work in a lazy language.