Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I just today finished this question in SICP, so I'll try to help here:</p> <p>I tested your code with this my-remove:</p> <pre><code>(define (my-remove num num-list) (cond ((null? num-list) nil) ((= num (car num-list)) (cdr num-list)) (else (cons (car num-list) (my-remove num (cdr num-list)))))) </code></pre> <p>tested with:</p> <pre><code>(let ((result (queens 8))) (display (length result))(newline) (map (lambda (possible-solution) (display possible-solution)(newline)) result) ) </code></pre> <p>returned 92 . . (3 7 2 8 5 1 4 6) (3 7 2 8 6 4 1 5) . .</p> <p>92 results, one of which is the solution in the book</p> <p>Here's a part of your code (slightly) refactored, I got rid of my-remove, and used a let*. I'm not going to refactor the whole thing, but I atleast tried to add meaning in the variable names. with these changes it was somewhat faster: (also added some comments)</p> <pre><code>;no major refactoring, essentially removed my-remove and some refactors (define (queens board-size) (define (safe? k position) (define (iter last-element front-lst col-num k) ;what's ok? (define (ok? l-e car-lst) (and (not (= l-e car-lst)) (not (= (abs (- l-e car-lst)) (abs (- k col-num)))))) (if (null? front-lst) true (and (ok? last-element (car front-lst)) (iter last-element (cdr front-lst) (++ col-num) k)))) ;queens means a list of rows, each representing a queen position ;that is, when queens[col] = row that means there's a queen in row 'row and ;column 'col ;reimplementing removing the queen, and the let turned to let* (let* ((reverse-column-queens (my-reverse position)) (last-queen (car reverse-column-queens)) ;remove that queen from the list ;btw 'position is actually 'positions (all-other-queens (my-reverse (cdr reverse-column-queens)))) (iter last-queen all-other-queens 1 k))) . . . </code></pre> <p>so, I guess the my-remove you used might have been wrong.</p> <p>Now, the names of your variables are not very discriptive, so it would add to an already confusing code (no offence meant to the author of this exercise or you).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload