<div dir="ltr"><div><div><div><div><div>Hi Dushyant,<br><br></div>The problem most likely is <br></div>[m | m <- [5,7..], m <= (truncate (sqrt (fromInteger x)))]<br><br></div> This is because, the filter condition (the last part) does a very simple thing:  It filters out any element that does not fulfil the criteria.  You are operating on a list that is monotonically increasing.  However, the filter isn't aware of this property.  Hence, this list comprehension never ends because it doesn't know that once the condition fails, it will always fail.<br><br></div><div>Thus, the solution would be to generate a finite set (or take a part of the infinite set using takeWhile or something like that), instead of using an infinite one.<br></div><div><br></div>Regards,<br></div>G Akash.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, May 5, 2016 at 6:13 PM, Dushyant Juneja <span dir="ltr"><<a href="mailto:juneja.dushyant@gmail.com" target="_blank">juneja.dushyant@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi,<div><br></div><div>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:</div><div><br></div><div><div>primesBelowN :: Integer -> [Integer]</div><div>primesBelowN n = 2:3:filter f [6*k+i | k <- [1..(n-1)`div`6], i <- [-1, 1]]</div><div>                     where f x = foldr g True xs</div><div>                                 where g t ac = (x `rem` t /= 0) && ac</div><div>                                       xs = [5, 7..(truncate (sqrt (fromInteger x)))]</div></div><div><br></div><div><br></div><div>However, the following never returns anything for the same number, probably due to some kind of loop malfunction:</div><div><br></div><div><div>primesBelowN :: Integer -> [Integer]</div><div>primesBelowN n = 2:3:filter f [6*k+i | k <- [1..(n-1)`div`6], i <- [-1, 1]]</div><div>                     where f x = foldr g True xs</div><div>                                 where g t ac = (x `rem` t /= 0) && ac</div><div>                                       xs = [ m | m <- [5, 7, ..], m <= <span style="line-height:1.5">(truncate (sqrt (fromInteger x)))</span><span style="line-height:1.5">]</span></div></div><div><br></div><div>Any ideas what might be going wrong?</div><div><br></div><div>Thanks in advance!</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>DJ</div></font></span></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>