summaryrefslogtreecommitdiff
path: root/miralib/ex/queens.m
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2022-03-04 12:32:20 -0500
committerJakob Kaivo <jkk@ung.org>2022-03-04 12:32:20 -0500
commit55f277e77428d7423ae906a8e1f1324d35b07a7d (patch)
tree5c1c04703dff89c46b349025d2d3ec88ea9b3819 /miralib/ex/queens.m
import Miranda 2.066 from upstream
Diffstat (limited to 'miralib/ex/queens.m')
-rw-r--r--miralib/ex/queens.m22
1 files changed, 22 insertions, 0 deletions
diff --git a/miralib/ex/queens.m b/miralib/ex/queens.m
new file mode 100644
index 0000000..2ed5734
--- /dev/null
+++ b/miralib/ex/queens.m
@@ -0,0 +1,22 @@
+||this generates all solutions to the 8 queens problem -- say
+|| solns
+||and it will print the solutions one per line - all 92 of them. This
+||is a good program for testing the garbage collector. Say `/gc' to
+||switch on garbage collector diagnostics.
+
+solns = layn(map show (queens 8))
+queens 0 = [[]]
+queens (n+1) = [q:b|b<-queens n;q<-[1..8];safe q b]
+safe q b = and[~checks q b i|i<-index b]
+checks q b i = q=b!i \/ abs(q-b!i)=i+1
+
+||Note that the function `queens n' returns a list of all solutions to
+||the n queens problem (placing queens in the first n columns of a chess
+||board, so that no queen gives check to another). A board with n
+||queens is represented as a list of n numbers, namely the positions of
+||the queens in each column
+
+||This example exhibits a basic technique of lazy functional
+||programming, which is to eliminate backtracking from a search problem
+||by working at the level of a list of all solutions, rather than a
+||single solution.