home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / hugs_1 / demos_hs_Queens < prev    next >
Encoding:
Text File  |  1996-08-12  |  838 b   |  23 lines

  1. -- This N-Queens program is based on a small variation of the 8-queens
  2. -- program from Bird and Wadler's book.
  3. --
  4. -- Be warned: printing out the complete list of solutions (all 92 of them)
  5. -- by evaluating "q 8" takes well over 1 million reductions and uses nearly
  6. -- 2.5 million cells... it may take some time to execute on slower systems! :-)
  7.  
  8. import Gofer
  9.  
  10. queens number_of_queens  = qu number_of_queens where
  11.     qu 0          = [[]]
  12.     qu (m+1)      = [ p++[n] | p<-qu m, n<-[1..number_of_queens], safe p n ]
  13.  
  14. safe p n          = all not [ check (i,j) (m,n) | (i,j) <- zip [1..] p ]
  15.                     where m = 1 + length p
  16.  
  17. check (i,j) (m,n) = j==n || (i+j==m+n) || (i-j==m-n)
  18.  
  19. -- Use q 5 to see the list of solutions for 5 queens.
  20. -- Use q 8 to see the list of solutions for 8 queens ....
  21. q = putStr . layn . map show . queens
  22.  
  23.