forked from felipap/sicp-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueens.rkt
27 lines (23 loc) · 795 Bytes
/
queens.rkt
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
#lang racket
(require racket/generator)
(define (safe? x y sln)
(if (null? sln)
#t
(let ((px (caar sln)) (py (cadar sln)))
(if (or (= y py) (= (- py y) (- px x)) (= (- py y) (- x px)))
#f
(safe? x y (cdr sln))))))
; input-> n, output-> a generator that yields
; every possible list of n queens' coordinates
(define (nqueen n)
(let recur ((x (- n 1)))
(generator ()
(if (= x -1)
(yield '())
(for* ([sln (in-producer (recur (- x 1)) (void))]
[y (range n)])
(and (safe? x y sln)
(yield (cons (list x y) sln)))))
(void))))
(for ([e (in-producer (nqueen 8) (void))])
(displayln e))