[Haskell-cafe] Floating phi, round and even Fibonnaci numbers

Brian L. Troutwine goofyheadedpunk at gmail.com
Tue Jul 10 17:11:13 EDT 2007


I'm rather new to Haskell and need, in typical newbie style,
a bit of help understanding the type system.

The Nth even Fibonacci number, EF(n) can be defined by the recursive
relation EF(0) = 2, EF(n) = [EF(n-1) * (phi**3)], where phi is the
golden ratio and [] is the nearest integer function. An infinite lazy
list of this sequence would be nice to have for my Project Euler, er,
project. Defining phi thusly,

> phi :: (Floating t) => t
> phi = (1+sqrt(5))/2

With phi in place, if I understood types properly (and if I understand
iterate correctly as I think), the lazy list should be a relatively
quick matter.

> even_fibs :: (Num t) => [t]
> even_fibs = iterate (\x -> round(x * (phi**3))) 2

Dynamically typed even_fibs :: (Floating t, Integral t, RealFrac t) =>
[t], assuming I pass -fno-monomorphism-restriction to ghci. That's not
at all the type I assumed even_fibs would take, as can be seen from
above. So, I went on a bit of sojourn. Having seen the sights of the
Haskell Report section 6.4, the marvels of the references cited in the
wiki's article on the monomorphism restriction and the Gentle
Introduction's chapter 10 I must say I'm rather more terribly confused
than when I started out, possibly.

Can someone explain where my type statements have gone wrong?


More information about the Haskell-Cafe mailing list