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

Tim Chevalier catamorphism at gmail.com
Tue Jul 10 17:18:41 EDT 2007


On 7/10/07, Brian L. Troutwine <goofyheadedpunk at gmail.com> wrote:
> 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.

That was your first mistake :-) (As a beginner, anyway.)

Look at the type of round:
Prelude> :t round
round :: forall a b. (RealFrac a, Integral b) => a -> b

So the argument x in the lambda-expression being passed to iterate
must have a type that's an instance of RealFrac. It must also have a
type that's an instance of Integral, since the result of multiplying
it with phi gets passed into the next iteration. Finally, it has to
have a type that's an instance of Floating, since phi is declared as a
Floating. You can probably see the problem.

Cheers,
Tim

-- 
Tim Chevalier* catamorphism.org *Often in error, never in doubt
"There's no money in poetry, but there's no poetry in money, either."
--Robert Graves


More information about the Haskell-Cafe mailing list