]> gerrit.simantics Code Review - simantics/platform.git/blob - docs/Developer/SCL/SCLExamples.md
Mapped dev-wiki conversion situation for situational awareness
[simantics/platform.git] / docs / Developer / SCL / SCLExamples.md
1 ## Eight queens puzzle
2
3 Copy the following commands to the SCL console:
4
5 ```haskell
6 > n = 8
7 > allPositions = [0..n-1]
8 > possibleNextPositions l = allPositions \\ (l + lq + uq)
9     where
10       m = length l
11       lq = [l!i - (m-i) | i <- [0..m-1]]
12       uq = [l!i + (m-i) | i <- [0..m-1]]
13 > expandSolution l = do
14       x <- possibleNextPositions l
15       return (l + [x])
16 > solve k cur = if k == n
17                 then return cur
18                 else expandSolution cur >>= solve (k+1)
19 > solutions = solve 0 []
20 > length solutions
21 > repeatString k (s :: String) = sum [s | x <- [1..k]]
22 > rowText k = repeatString k "." + "X" + repeatString (n-k-1) "."
23 > printSolution (l :: [Integer]) = do
24       printString (repeatString n "-")
25       mapM (printString . rowText) l
26 > mapM printSolution solutions
27 ```