-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise2-42.lisp
51 lines (44 loc) · 1.53 KB
/
exercise2-42.lisp
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
(define (flatmap proc seq)
(foldr append '() (map proc seq)))
(define (enumerate-interval low high)
(if (> low high)
'()
(cons low (enumerate-interval (+ low 1) high))))
(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-interval 1 board-size)))
(queen-cols (- k 1))))))
(queen-cols board-size))
(define empty-board '())
(define (adjoin-position new-row k rest-of-queens)
(if (null? rest-of-queens)
(list new-row)
(map (lambda (old-position) (flatten (cons old-position new-row))) rest-of-queens)))
(define (safe-pair? board i j)
(let ((delta (- j i))
(ival (list-ref board (- i 1)))
(jval (list-ref board (- j 1))))
(not (or (= ival jval)
(= ival (- jval delta))
(= ival (+ jval delta))))))
(define (safe-position? board k)
(foldr (lambda (x y) (and x y))
#t
(flatmap (lambda (i) (map (lambda (j) (safe-pair? board i j))
(range (+ i 1) (+ k 1))))
(range 1 (+ k 1)))))
(define (safe? k positions)
(foldr (lambda (x y) (and x y))
#t
(map (lambda (position) (safe-position? position k))
positions)))
(display (queens 8))
(newline)