[Haskell-cafe] Fibonacci numbers generator in Haskell

Chris Kuklewicz haskell at list.mightyreason.com
Fri Jun 16 09:28:37 EDT 2006


Chris Kuklewicz wrote:
> Mathew Mills wrote:
>> I guess I don't get any points for an approximate solution, ay?
>>
>> Is there anything that can be done (easily) to reduce the rounding 
>> errors?
>>
>>
> http://www.google.com/search?q=haskell+exact+real+arithmetic
> 

Using Era.hs (with the patch at 
http://www.haskell.org/hawiki/ExactRealArithmetic) and add spaces around 1%n to 
1 % n (since I have -fglasgow-exts turned on and %n looks like a linear implicit 
thingie).


Now this works:

> fib x = let phi,phi' :: CR
>             phi = (1 + sqrt 5) /2
>             phi' = (1 - sqrt 5)/2
>         in  truncate ( recip (sqrt 5) * (phi^x -phi'^x))

*Era> fib 100
354224848179261915075

which is the (allegedly) correct answer.


-- 
Chris


More information about the Haskell-Cafe mailing list