X-No-Archive:
i woz lookn thru SICP nd i cin diz 8 kweenz puzzl!! lol!!!
http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-15.html#%_sec_2.2.3
da nx wk i wrk on exasize 2.42 nd 2.43. itz hard nd stuff cuz
no 1 eva sez da answer nd i think exasize 2.43 izza trick kwestshun
nd alelson nd sussman just wanna b kool like mean cats r kool!!!!
i think that exasize 2.42 izza exponential process and da mods 4
exasize 2.43 izza exponential process to. der iz no difference in
da speed. cud som 1 plz confirm dis
thats all i haf to say so i guess ill go hang my self with my socks
bye bye
;; my soln to 2.42
;; SICP (2ed.) pg 125
(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))
;; list of prospective board positions for new queen
(define (adjoin-position new-row k rest-of-queens)
(append rest-of-queens (list (list new-row k))))
; a predicate to determine if two queens threaten each other
; (from Lisp 3ed, Winston and Horn pg289)
(define (threat? i j a b) ; One queen at (i,j) other at (a,b)
(or (= i a) ; same row
(= j b) ; same column
(= (- i j) (- a b)) ; SW-NE diagonal
(= (+ i j) (+ a b)))) ; NW-SE diagonal
; a predicate to tell us if the position (n,m) for the new
; queen is safe (from Winston and Horn pg289)
(define (conflict? n m board)
(cond ((null? board) board)
((threat? n
m
(car (car board))
(cadr (car board)))
#t)
(else (conflict? n m (cdr board)))))
(define (safe? k positions)
(let ((nk-pair (car (last-pair positions))) ; new queen
(k-minus-1-board (except-last-pair positions))) ; other queens
(or (= k 1) ; no conflicts if only one queen
(not (conflict? (car nk-pair) k k-minus-1-board)))))
(define empty-board ())
(define (filter predicate sequence)
(cond ((null? sequence) sequence)
((predicate (car sequence))
(cons (car sequence)
(filter predicate (cdr sequence))))
(else (filter predicate (cdr sequence)))))
(define (flatmap proc seq)
(fold-right append () (map proc seq)))
(define (enumerate-interval low high)
(if (> low high)
()
(cons low (enumerate-interval (+ low 1) high))))
;; end of my soln