[Haskell-beginners] Infinite recursion in list comprehension
Dushyant Juneja
juneja.dushyant at gmail.com
Thu May 5 12:43:02 UTC 2016
Hi,
I seem to be landing into infinite recursion when using higher order
functions with list comprehension. Take this for an example. The following
works well, and gives answers for numbers like 2000000 as well:
primesBelowN :: Integer -> [Integer]
primesBelowN n = 2:3:filter f [6*k+i | k <- [1..(n-1)`div`6], i <- [-1, 1]]
where f x = foldr g True xs
where g t ac = (x `rem` t /= 0) && ac
xs = [5, 7..(truncate (sqrt
(fromInteger x)))]
However, the following never returns anything for the same number, probably
due to some kind of loop malfunction:
primesBelowN :: Integer -> [Integer]
primesBelowN n = 2:3:filter f [6*k+i | k <- [1..(n-1)`div`6], i <- [-1, 1]]
where f x = foldr g True xs
where g t ac = (x `rem` t /= 0) && ac
xs = [ m | m <- [5, 7, ..], m
<= (truncate
(sqrt (fromInteger x)))]
Any ideas what might be going wrong?
Thanks in advance!
DJ
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160505/3999ede8/attachment.html>
More information about the Beginners
mailing list