]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - docs/Developer/SCL/SCLExamples.md
Mapped dev-wiki conversion situation for situational awareness
[simantics/platform.git] / docs / Developer / SCL / SCLExamples.md
diff --git a/docs/Developer/SCL/SCLExamples.md b/docs/Developer/SCL/SCLExamples.md
new file mode 100644 (file)
index 0000000..5172625
--- /dev/null
@@ -0,0 +1,27 @@
+## Eight queens puzzle
+
+Copy the following commands to the SCL console:
+
+```haskell
+> n = 8
+> allPositions = [0..n-1]
+> possibleNextPositions l = allPositions \\ (l + lq + uq)
+    where
+      m = length l
+      lq = [l!i - (m-i) | i <- [0..m-1]]
+      uq = [l!i + (m-i) | i <- [0..m-1]]
+> expandSolution l = do
+      x <- possibleNextPositions l
+      return (l + [x])
+> solve k cur = if k == n
+                then return cur
+                else expandSolution cur >>= solve (k+1)
+> solutions = solve 0 []
+> length solutions
+> repeatString k (s :: String) = sum [s | x <- [1..k]]
+> rowText k = repeatString k "." + "X" + repeatString (n-k-1) "."
+> printSolution (l :: [Integer]) = do
+      printString (repeatString n "-")
+      mapM (printString . rowText) l
+> mapM printSolution solutions
+```
\ No newline at end of file