All pastes #265928 Raw Edit

Stuff

public text v1 · immutable
·published 2006-12-03 19:35 UTC
rendered paste body
(define (find-route orig dest G)
  (local ((define (neighbours v G)
            (cond
              [(empty? G) (error 'neighbours (string-append (symbol->string v) " not in graph"))]
              [else
               (cond
                 [(symbol=? v (first (first G))) (second (first G))]
                 [else (neighbours v (rest G))])]))
          (define (find-route-helper orig dest seen still-to-visit G)
            (cond
              [(empty? G) false]
              [(member dest seen) (reverse seen)]
              [(empty? (neighbours orig G)) false]
              [else (find-route-helper (first (neighbours orig G)) 
                                       dest 
                                       (append (list (first (neighbours (first seen) G))) seen) 
                                       (append (rest (neighbours orig G)) still-to-visit) (rest G))])))  
  (find-route-helper orig dest (list orig) empty G)))

(define testGraph '((A (B1 B2)) (B1 (C)) (B2 (C)) (C (D1 D2)) (D1 (E)) (D2 (E)) (E ()) (F ()))) 
(equal? (find-route 'A 'F testGraph) false)
(equal? (find-route 'A 'C testGraph) '(A B1 C))