; ; swap --- swaps elements numbered x and y (0 referenced) in the list l ; ; this version of swap uses the technique of defining a function ; within a function which we covered in class Thurs 9/17/99 ; ; note that there is no error checking --- if x or y is out of range, ; we'll get an error from list-ref ; (define (swap l x y) (let ((y-el (list-ref l y)) (x-el (list-ref l x))) (define (swap-helper lst n) (cond ((null? lst) '()) ((= n x) (cons y-el (swap-helper (cdr lst) (+ n 1)))) ((= n y) (cons x-el (swap-helper (cdr lst) (+ n 1)))) (else (cons (car lst) (swap-helper (cdr lst) (+ n 1)))))) (swap-helper l 0))) ; ; 8-puzzle children generator assuming the representation ; ; (1 2 3 4 5 6 7 8 space) ; (define (ep-children s) (let ((sp-pos (- 9 (length (member 'space s))))) (map (lambda (pos) (swap s pos sp-pos)) (case sp-pos ((0) '(1 3)) ((1) '(0 2 4)) ((2) '(1 5)) ((3) '(0 4 6)) ((4) '(1 3 5 7)) ((5) '(2 4 8)) ((6) '(3 7)) ((7) '(4 6 8)) ((8) '(5 7))))))