[Haskell-beginners] Generating Infinite List of Fibonacci Numbers

Desonte Jolivet jolivetdesonte at yahoo.com
Wed Jun 29 03:39:23 UTC 2016


Hi,

Problem: I would like to generate an infinite list of Fibonacci numbers. Although the below code does not work.

fibs :: [Integer]fibs = 0 : 1 : [ n | x <-[2..], let n = ((fibs !! x-1) + (fibs !! x-2))]

Thought: I'm assuming that I'm ignorant on how ranges/generators work with a list comprehension, which is why this code is not working.

ghci output:
*Main> fibs !! 01*Main> fibs !! 12*Main> fibs !! 2 
When I try and access the third element in the list ghci simply stalls.
Can someone please give me a little more insight on why my code is not working.
Thanks in advance.
drjoliv
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160629/6dcb89ac/attachment-0001.html>


More information about the Beginners mailing list