I am working on creating a list of all the even numbers in the Fibonacci series which are less than or equal to 4,000,000. In Haskell, I've defined the Fibonacci series as:
fibs = 1 : 2 : next fibs
where
next (a : t@(b:_)) = (a+b) : next t
and am using the following list comprehension to construct my set:
[ x | x <- take 50 fibs, x `mod` 2 == 0, last x <= 4*10^6 ]
However, GHC is throwing a Couldn't match expected type ‘[Integer]’ with actual type ‘Integer’
error.
I understand that the predicate, last x <= 4*10^6
, is responsible for the error. Inspired by hammar's answer here, my initial reaction was to ensure that 4*10^6
was the right type, so I tried rephrasing the predicate as last x <= toInteger 4*10^6
to no avail; same error. I also thought that maybe I needed to specify 4*10^6
as a singleton (i.e. [4*10^6]
), but no luck there either.
I'm struggling to understand what is exactly going on and how best to resolve the issue.