-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_2_43.clj
54 lines (51 loc) · 2.1 KB
/
ex_2_43.clj
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
(ns sicp.chapter-2.part-2.ex-2-43
(:require
[sicp.chapter-2.part-2.book-2-2 :as b22]
[sicp.chapter-2.part-2.ex-2-42 :as ex-2-42]))
; Exercise 2.43
;
; Louis Reasoner is having a terrible time doing Exercise 2.42.
; His queens procedure seems to work, but it runs extremely slowly.
; (Louis never does manage to wait long enough for it to solve even the 6×6 case.)
; When Louis asks Eva Lu Ator for help, she points out that he has interchanged
; the order of the nested mappings in the flatmap, writing it as
;
; (flatmap
; (lambda (new-row)
; (map (lambda (rest-of-queens)
; (adjoin-position
; new-row k rest-of-queens))
; (queen-cols (- k 1))))
; (enumerate-interval 1 board-size))
;
; Explain why this interchange makes the program run slowly.
; Estimate how long it will take Louis’s program to solve the eight-queens puzzle,
; assuming that the program in Exercise 2.42 solves the puzzle in time T.
(defn queens-2-42
[board-size]
(letfn [(queen-cols
[k]
(if (= 0 k)
(list nil)
(filter (fn [positions] (ex-2-42/safe? k positions))
(mapcat (fn [rest-of-queens]
(map (fn [new-row]
(ex-2-42/adjoin-position new-row k rest-of-queens))
(b22/enumerate-interval 1 board-size)))
(queen-cols (- k 1))))))]
(queen-cols board-size)))
(defn queens-2-43
[board-size]
(letfn [(queen-cols
[k]
(if (= 0 k)
(list nil)
(filter (fn [positions] (ex-2-42/safe? k positions))
(mapcat (fn [new-row]
; diff 1
(map (fn [rest-of-queens]
; diff 1
(ex-2-42/adjoin-position new-row k rest-of-queens))
(queen-cols (- k 1)))) ; diff 2
(b22/enumerate-interval 1 board-size)))))] ; diff 2
(queen-cols board-size)))