Difference between revisions of "99 questions/90 to 94"

From HaskellWiki
Jump to navigation Jump to search
m
Line 9: Line 9:
 
== Problem 90 ==
 
== Problem 90 ==
   
  +
Eight queens problem
<Problem description>
 
   
  +
This is a classical problem in computer science. The objective is to place eight queens on a chessboard so that no two queens are attacking each other; i.e., no two queens are in the same row, the same column, or on the same diagonal.
<pre>
 
Example:
 
<example in lisp>
 
   
  +
Hint: Represent the positions of the queens as a list of numbers 1..N. Example: [4,2,7,3,6,8,5,1] means that the queen in the first column is in row 4, the queen in the second column is in row 2, etc. Use the generate-and-test paradigm.
  +
 
<pre>
 
Example in Haskell:
 
Example in Haskell:
  +
> length queens
<example in Haskell>
 
  +
92
  +
> take 1 queens
  +
[[3,1,6,2,5,7,4,0]]
 
</pre>
 
</pre>
   
 
Solution:
 
Solution:
 
<haskell>
 
<haskell>
  +
queens = queens' 8
<solution in haskell>
 
  +
  +
queens' 0 = [[]]
  +
queens' (n+1) = [ try:alreadySet | alreadySet <- queens' n, try <- [1..8], isSafe try alreadySet ]
  +
where isSafe try alreadySet = not . any (threatens try alreadySet) $ [1..n]
  +
threatens try alreadySet col = let diff = try - alreadySet!!(col-1) in (diff == 0) || (diff == col)
 
</haskell>
 
</haskell>
   
  +
By definition/data representation no two queens can occupy the same column. "diff == 0" checks for a queen in the same row, "abs(diff) == col" checks for a queen in the same diagonal.
<description of implementation>
 
  +
  +
This is a modification of a function I wrote when I was just learning haskell, so there's certainly much to improve here! For one thing there is speedup potential in caching "blocked" rows, columns and diagonals.
 
 
 
== Problem 91 ==
 
== Problem 91 ==

Revision as of 04:14, 13 December 2006


These are Haskell translations of Ninety Nine Lisp Problems.

If you want to work on one of these, put your name in the block so we know someone's working on it. Then, change n in your block to the appropriate problem number, and fill in the <Problem description>,<example in lisp>,<example in Haskell>,<solution in haskell> and <description of implementation> fields.

Miscellaneous problems

Problem 90

Eight queens problem

This is a classical problem in computer science. The objective is to place eight queens on a chessboard so that no two queens are attacking each other; i.e., no two queens are in the same row, the same column, or on the same diagonal.

Hint: Represent the positions of the queens as a list of numbers 1..N. Example: [4,2,7,3,6,8,5,1] means that the queen in the first column is in row 4, the queen in the second column is in row 2, etc. Use the generate-and-test paradigm.

Example in Haskell:
> length queens
92
> take 1 queens
[[3,1,6,2,5,7,4,0]]

Solution:

queens = queens' 8

queens' 0     = [[]]
queens' (n+1) = [ try:alreadySet | alreadySet <- queens' n, try <- [1..8], isSafe try alreadySet ]
    where isSafe try alreadySet        = not . any (threatens try alreadySet) $ [1..n]
          threatens try alreadySet col = let diff = try - alreadySet!!(col-1) in (diff == 0) || (diff == col)

By definition/data representation no two queens can occupy the same column. "diff == 0" checks for a queen in the same row, "abs(diff) == col" checks for a queen in the same diagonal.

This is a modification of a function I wrote when I was just learning haskell, so there's certainly much to improve here! For one thing there is speedup potential in caching "blocked" rows, columns and diagonals.

Problem 91

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 92

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 93

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 94

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 95

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 96

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 97

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 98

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 99

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>