;; find-route : node node graph -> (listof node) or false ;; to create a path from origination to destination in G ;; if there is no path, the function produces false (define (find-route origination destination G) (cond [(symbol=? origination destination) (list destination)] [else (local ((define possible-route (find-route/list (neighbors origination G) destination G))) (cond [(boolean? possible-route) false] [else (cons origination possible-route)]))])) #| neighbors: node graph -> (listof node) (define (neighbors a-node a-graph) ...) Purpose: compute a-node's neighbors in a-graph Example: in figure~xyz, G has no neighbors: empty A has the list of neighbors (list 'B 'E) |# (define (neighbors a-node a-graph) (cond ((empty? a-graph) (error 'neighbors "can't happen")) (else (cond ((eq? (first (first a-graph)) a-node) (second (first a-graph))) (else (neighbors a-node (rest a-graph))))))) ;; find-route/list : (listof node) node graph -> (listof node) or false ;; to create a path from some node on lo-Os to D ;; if there is no path, the function produces false (define (find-route/list lo-Os D G) (cond [(empty? lo-Os) false] [else (local ((define possible-route (find-route (first lo-Os) D G))) (cond [(boolean? possible-route) (find-route/list (rest lo-Os) D G)] [else possible-route]))])) (define GRAPH '((A (B C)) (B (C D)) (C (D)) (D ()))) (equal? (find-route 'A 'D GRAPH) '(A B C D)) (equal? (find-route 'A 'A GRAPH) '(A)) (equal? (find-route 'C 'B GRAPH) false) ;(define CYCLIC ; '((A (A)))) ; ;(find-route 'A 'C CYCLIC)