;; a helper file for lab9-library.ss ;; Searching a graph. ;; ;;(define-structure (city name nbhrs seen?)) (define already-seen null) (define (have-seen2? c) (city-in-list? c already-seen)) (define (mark-seen2! c) (set! already-seen (cons c already-seen))) (define (reachable2? here there) (begin (mark-seen2! here) (if (same-city? here there) #t (reachable2-from? (city-nbhrs here) there)))) (define (reachable2-from? loc there) (cond ((null? loc) #f) ((if (have-seen2? (car loc)) #f (reachable2? (car loc) there)) #t) (else (reachable2-from? (cdr loc) there)))) ;; (forget2) ;; allow reachable2? to be called again ;; (define (forget2) (set! already-seen null)) (define (city-in-list? c loc) (cond ((null? loc) #f) (else (if (same-city? c (car loc)) #t (city-in-list? c (cdr loc)))))) ;; ------- a printing version -------- ;; (print the list already-seen every time ;; we check it) ;; Buggy to click "stop executing" while (a library function) ;; is printing? ;; (define (names-of loc) (map city-name loc)) (define (names-seen) (names-of already-seen)) (define (have-seen2p? c) (begin (printf "Looking for ~s in ~s~n~n" (city-name c) (names-seen)) (city-in-list? c already-seen))) (define (reachable2p? here there) (begin (mark-seen2! here) (if (same-city? here there) #t (reachable2p-from? (city-nbhrs here) there)))) (define (reachable2p-from? loc there) (cond ((null? loc) #f) ((if (have-seen2p? (car loc)) #f (reachable2p? (car loc) there)) #t) (else (reachable2p-from? (cdr loc) there))))