-
Notifications
You must be signed in to change notification settings - Fork 0
/
eight-queens.scm
57 lines (49 loc) · 1.44 KB
/
eight-queens.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
(load "flatmap.scm")
(load "enumerate.scm")
(define (queens board-size)
(define (queen-cols k)
(if (= k 0)
(list empty-board)
(filter
(lambda (positions) (safe? k positions))
(flatmap
(lambda (rest-of-queens)
(map (lambda (new-row)
(adjoin-position new-row k rest-of-queens))
(enumerate 1 board-size)))
(queen-cols (- k 1))))))
(queen-cols board-size))
; represent positions by a list of (queen_row, queen_col) pairs
(define empty-board '())
(define (adjoin-position new-row k rest-of-queens)
(cons (list new-row k) rest-of-queens)
)
(define (safe? k positions)
; adjoin-position always prepends the position of the queen in the k-th column
(define (in-check? queen1 queen2)
(let (
(r1 (car queen1))
(r2 (car queen2))
(c1 (cadr queen1))
(c2 (cadr queen2))
)
(cond ((= r1 r2) #f)
((= c1 c2) #f)
((= (abs (- r1 r2)) (abs (- c1 c2))) #f)
(else #t)
)
)
)
(let (
(queen-to-check (car positions))
(rest (cdr positions))
)
(accumulate (lambda (queen others) (and (in-check? queen-to-check queen) others)) #t rest)
)
)
(load "for-each.scm")
(define (display-solution solution)
(newline)
(display solution)
)
(for-each display-solution (queens 8))