Following code from http://htdp.org/2003-09-26/Book/curriculum-Z-H-38.html#node_chap_30 does'nt seem to be working (I have added println statements for debugging)
(define (neighbor a-node sg)
(println "in neighbor fn")
(cond
[(empty? sg) (error "neighbor: impossible")]
[else (cond
[(symbol=? (first (first sg)) a-node)
(second (first sg))]
[else (neighbor a-node (rest sg))])]))
(define (route-exists? orig dest sg)
(println "in route-exits? fn")
(cond
[(symbol=? orig dest) true]
[else (route-exists? (neighbor orig sg) dest sg)]))
I also tried the second version (I have added contains fn):
(define (contains item sl)
(ormap (lambda (x) (equal? item x)) sl) )
(define (route-exists2? orig dest sg)
(local ((define (re-accu? orig dest sg accu-seen)
(cond
[(symbol=? orig dest) true]
[(contains orig accu-seen) false]
[else (re-accu? (neighbor orig sg) dest sg (cons orig accu-seen))])))
(re-accu? orig dest sg empty)))
Following example from that page itself creates infinite loop:
(route-exists? 'C 'D '((A B) (B C) (C E) (D E) (E B) (F F)))
Following produces #f even though solution is clearly possible:
(route-exists2? 'C 'D '((A B) (B C) (C E) (D E) (E B) (F F)))
Following tests (lists are mine) produce errors here also even though solution is clearly possible:
(route-exists?
'A 'C
'('('A 'B) '('B 'C) '('A 'C) '('A 'D) '('B 'E) '('E 'F) '('B 'F) '('F 'G) )
)
(route-exists2?
'A 'C
'('('A 'B) '('B 'C) '('A 'C) '('A 'D) '('B 'E) '('E 'F) '('B 'F) '('F 'G) )
)
Where is the problem and how can this be solved?
Edit:
Even after choosing the new function and remove excess quotes, following does not work (even though there is a direct path between A and D):
(route-exists2?
'A 'D
'((A B) (B C) (A C) (A D) (B E) (E F) (B F) (F G) ) )
It outputs an error:
neighbor: impossible