<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"><<a href="mailto:efiish@gmail.com" target="_blank">efiish@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">Thanks again! I understand now. I'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"><<a href="mailto:artyom.kazak@gmail.com" target="_blank">artyom.kazak@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Junior White <<a href="mailto:efiish@gmail.com" target="_blank">efiish@gmail.com</a>> писал(а) в своём письме 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>
Thanks! But I don't understand why in the first case "queens' (k-1)" is<br>
being recomputed n times?<br>
</blockquote>
<br>
Because your list comprehension is just a syntactic sugar for<br>
<br>
concatMap (\q -><br>
concatMap (\qs -> if isSafe q qs then [q:qs] else [])<br>
(queens' (k-1)))<br>
[1..n]<br>
<br>
Here `queens' (k-1)` does not depend on `qs`, and therefore it *could* be floated out of the lambda:<br>
<br>
let queens = queens' (k-1)<br>
in<br>
concatMap (\q -><br>
concatMap (\qs -> if isSafe q qs then [q:qs] else [])<br>
queens)<br>
[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’s why GHC leaves this to you.<br>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>