<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Jan 30, 2013 at 5:51 PM, Doaitse Swierstra <span dir="ltr"><<a href="mailto:doaitse@swierstra.net" target="_blank">doaitse@swierstra.net</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">From the conclusion that both programs compute the same result it can be concluded that the fact that you have made use of a list comprehension has forced you to make a choice which should not matter, i.e. the order in which to place the generators. This should be apparent from your code.<div>
<br></div><div>My approach is such a situation is to "define your own generator" (assuming here that isSafe needs both its parameters):</div><div><br></div><div>pl `x` ql = [ (p,q) | p <-pl, q <- ql]</div>
<div><br></div><div>queens3 n = map reverse $ queens' n</div><div> where queens' 0 = [[]] </div><div> queens' k = [q:qs | (qs, q) <- queens' (k-1) `x` [1..n], isSafe q qs] </div>
<div class="im"><div> isSafe try qs = not (try `elem` qs || sameDiag try qs) </div></div><div> sameDiag try qs = any (\(colDist,q) -> abs (try - q) == colDist) $ zip [1..] qs</div>
<div><br></div><div>Of course you can make more refined versions of `x`, which perform all kinds of fair enumeration, but that is not the main point here. It is the fact that the parameters to `x` are only evaluated once which matters here.</div>
</div></blockquote><div><br></div><div style>Thanks for your reply! I must learn more to fully understand what's going on inside the list comprehension. </div><div style>But when I frist learn Haskell, it says sequence doesn't matter, but now it is a big matter, can compiler do some thing for us? I think this behavior is not friendly to newbies like me, I will take a very long time to work through it.</div>
</div></div></div>