Ticket #101 (closed defect: invalid)

Opened 5 years ago

Last modified 5 years ago

Lazy list still results in stack overflow

Reported by: erikd@… Owned by:
Priority: normal Milestone:
Component: Build System Version:
Keywords: Cc:

Description

Simple Sieve of Eratosthenes :

primes = sieve $ rangeInfIntL 2

sieve (p : xs) =

p : sieve [ x | x <- xs, x mod p > 0 ]

main =

print $ show $ take 30 primes

when run results in:

*** DDC RTS PANIC! Slot stack overflow. Aborted (core dumped)

Change History

Changed 5 years ago by benl

  • status changed from new to closed
  • resolution set to invalid

If you create an infinite list you have to be careful not to demand it all at once.

For this one, suspend the application of sieve to the list, and generate the list comprehension in a lazy way (not the <@- operator)

primes = sieve $ rangeInfIntL 2

sieve (p : xs)
	= p : sieve @ [ x | x <@- xs, x `mod` p /= 0 ]

main () =
	print $ show $ take 30 primes
Note: See TracTickets for help on using tickets.