-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnqueens.scm
More file actions
34 lines (26 loc) · 819 Bytes
/
Copy pathnqueens.scm
File metadata and controls
34 lines (26 loc) · 819 Bytes
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
;;; NQUEENS -- Compute number of solutions to 8-queens problem.
(import (rnrs))
(define trace? #f)
(define (nqueens n)
(define (iota1 n)
(let loop ((i n) (l '()))
(if (= i 0) l (loop (- i 1) (cons i l)))))
(define (my-try x y z)
(if (null? x)
(if (null? y)
(begin
(when trace? (begin (write z) (newline)))
1)
0)
(+ (if (ok? (car x) 1 z)
(my-try (append (cdr x) y) '() (cons (car x) z))
0)
(my-try (cdr x) (cons (car x) y) z))))
(define (ok? row dist placed)
(if (null? placed)
#t
(and (not (= (car placed) (+ row dist)))
(not (= (car placed) (- row dist)))
(ok? row (+ dist 1) (cdr placed)))))
(my-try (iota1 n) '() '()))
(nqueens 14)