home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / hugs101.zip / hugs101sc.zip / hugsdist / demos / queens.hs < prev    next >
Text File  |  1995-02-14  |  819b  |  21 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. queens number_of_queens  = qu number_of_queens where
  9.     qu 0          = [[]]
  10.     qu (m+1)      = [ p++[n] | p<-qu m, n<-[1..number_of_queens], safe p n ]
  11.  
  12. safe p n          = all not [ check (i,j) (m,n) | (i,j) <- zip [1..] p ]
  13.                     where m = 1 + length p
  14.  
  15. check (i,j) (m,n) = j==n || (i+j==m+n) || (i-j==m-n)
  16.  
  17. -- Use q 5 to see the list of solutions for 5 queens.
  18. -- Use q 8 to see the list of solutions for 8 queens ....
  19. q n = layn (map show (queens n))
  20.  
  21.