<div dir="ltr">Hi Cafe,<div style> I have two programs for the same problem "<span style="color:rgb(0,0,0);font-family:'Helvetica Neue',Arial,Helvetica,sans-serif;font-size:medium;line-height:17.600000381469727px">Eight queens problem",</span></div>
<div style>the link is <a href="http://www.haskell.org/haskellwiki/99_questions/90_to_94">http://www.haskell.org/haskellwiki/99_questions/90_to_94</a>.</div><div style> My two grograms only has little difference, but the performance, this is my solution:</div>
<div style><br></div><div style>-- solution 1------------------------------------------------------------</div><div style><div>queens1 :: Int -> [[Int]] </div>
<div>queens1 n = map reverse $ queens' n </div><div> where queens' 0 = [[]] </div>
<div> queens' k = [q:qs | q <- [1..n], qs <- queens' (k-1), isSafe q qs] </div><div> isSafe try qs = not (try `elem` qs || sameDiag try qs) </div>
<div> sameDiag try qs = any (ë(colDist, q) -> abs (try - q) == colDist) $ zip [1..] qs </div><div><br></div><div style>-- solution 2--------------------------------------------------------------</div><div style>
<div>queens2 :: Int -> [[Int]] </div><div>queens2 n = map reverse $ queens' n </div>
<div> where queens' 0 = [[]] </div><div> queens' k = [q:qs | qs <- queens' (k-1), q <- [1..n], isSafe q qs] </div>
<div> isSafe try qs = not (try `elem` qs || sameDiag try qs) </div><div> sameDiag try qs = any (ë(colDist,q) -> abs (try - q) == colDist) $ zip [1..] qs </div>
<div><br></div><div style>the performance difference is: (set :set +s in ghci)</div><div style><div>*Main> length (queens1 8)</div><div>92</div><div>(287.85 secs, 66177031160 bytes)</div><div>*Main> length (queens2 8)</div>
<div>92</div><div>(0.07 secs, 17047968 bytes)</div><div>*Main> </div><div><br></div><div style>The only different in the two program is in the first is "q <- [1..n], qs <- queens' (k-1)," and the second is "qs <- queens' (k-1), q <- [1..n]".</div>
<div style><br></div><div style>Does sequence in list comprehansion matter? And why?</div></div></div></div></div>