<div dir="ltr">So this is a problem in lazy evaluation language, it will not appear in python or erlang, am i right?</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Jan 29, 2013 at 5:54 PM, Junior White <span dir="ltr">&lt;<a href="mailto:efiish@gmail.com" target="_blank">efiish@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Thanks again! I understand now. I&#39;ll be careful when the next time I use list comprehension.</div><div class="HOEnZb">
<div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Jan 29, 2013 at 5:48 PM, Artyom Kazak <span dir="ltr">&lt;<a href="mailto:artyom.kazak@gmail.com" target="_blank">artyom.kazak@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Junior White &lt;<a href="mailto:efiish@gmail.com" target="_blank">efiish@gmail.com</a>&gt; писал(а) в своём письме Tue, 29 Jan 2013 12:40:08 +0300:<br>


<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi Artyom,<br>
&nbsp; &nbsp;Thanks! But I don&#39;t understand why in the first case &quot;queens&#39; (k-1)&quot; is<br>
being recomputed n times?<br>
</blockquote>
<br>
Because your list comprehension is just a syntactic sugar for<br>
<br>
&nbsp; &nbsp; concatMap (\q -&gt;<br>
&nbsp; &nbsp; &nbsp; concatMap (\qs -&gt; if isSafe q qs then [q:qs] else [])<br>
&nbsp; &nbsp; &nbsp; &nbsp; (queens&#39; (k-1)))<br>
&nbsp; &nbsp; &nbsp; [1..n]<br>
<br>
Here `queens&#39; (k-1)` does not depend on `qs`, and therefore it *could* be floated out of the lambda:<br>
<br>
&nbsp; &nbsp; let queens = queens&#39; (k-1)<br>
&nbsp; &nbsp; in<br>
&nbsp; &nbsp; concatMap (\q -&gt;<br>
&nbsp; &nbsp; &nbsp; concatMap (\qs -&gt; if isSafe q qs then [q:qs] else [])<br>
&nbsp; &nbsp; &nbsp; &nbsp; queens)<br>
&nbsp; &nbsp; &nbsp; [1..n]<br>
<br>
But it is an unsafe optimisation. Suppose that the `queens` list is very big. If we apply this optimisation, it will be retained in memory during the whole evaluation, which may be not desirable. That&rsquo;s why GHC leaves this to you.<br>


</blockquote></div><br></div>
</div></div></blockquote></div><br></div>