[Haskell-cafe] Floating phi, round and even Fibonnaci numbers
haskell at list.mightyreason.com
haskell at list.mightyreason.com
Tue Jul 10 17:25:50 EDT 2007
Brian L. Troutwine wrote:
>> phi :: (Floating t) => t
>> phi = (1+sqrt(5))/2
>
>
>> even_fibs :: (Num t) => [t]
>> even_fibs = iterate (\x -> round(x * (phi**3))) 2
*Main> :t iterate
iterate :: forall a. (a -> a) -> a -> [a]
*Main> :t round
round :: forall a b. (RealFrac a, Integral b) => a -> b
So the 'x' in your anonymous lambda must be (a->a) and the type 'a' must be an
integral.
You need to convert this Integral 'x' into something that can be used in the
math (x * (phi**3)) which is where you need to insert fromIntegral:
> even_fibs :: (Integral t) => [t]
> even_fibs = iterate (\x -> round(fromIntegral x * (phi**3))) 2
>
Which of course can be tested against
> even_fibs_2 = filter even fibs
> where fibs = 1 : 1 : zipWith (+) (fibs) (tail fibs)
And the phi version fails at
> head $ dropWhile (uncurry (==)) $ zip even_fibs even_fibs_2
Which is
(37889062373143904,37889062373143906)
More information about the Haskell-Cafe
mailing list