1 ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 ;; about the language level of this file in a form that our tools can easily process.
3 #reader(lib "htdp-advanced-reader.ss" "lang")((modname |30.2|) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp")))))
7 ;(cons n1 (cons n2)), ie, (list n1 n2)
8 ;where n1 and n2 are symbols representing nodes.
10 ;A simple-graph is a (listof pairs).
12 ;route-exists? : node node simple-graph -> boolean
13 ;Determines if there exists a route from orig to dest given the simple-graph sg.
15 ;route-exists?-aux : node (listof nodes) -> boolean
16 ;Determine if there exists a route from orig to dest given the simple-graph sg. accu-seen represents nodes that have been visited before. If a node is visited twice and a route is not determined, return false.
18 (define (route-exists? orig dest sg)
19 (local ((define (route-exists?-aux orig accu-seen)
21 [(symbol=? orig dest) true]
22 [(contains orig accu-seen) false]
23 [else (route-exists?-aux (neighbor orig sg) (cons orig accu-seen))])))
24 (route-exists?-aux orig empty)))
26 ;neighbor : node simple-graph -> node
27 ;Given anode, find its neighbor in simple-graph.
29 (define (neighbor anode sg)
31 [(empty? sg) (error 'neighbor "node does not exist")]
32 [(symbol=? (first (first sg)) anode) (second (first sg))]
33 [else (neighbor anode (rest sg))]))
35 ;contains : X (listof X) -> boolean
36 ;Determines if alox contains x.
38 (define (contains x alox)
39 (ormap (lambda (item) (equal? x item)) alox))
44 [(equal? x (first alox)) true]
45 [else (contains x (rest alox))]))
55 (not (route-exists? 'A 'D SimpleG))
56 (route-exists? 'D 'B SimpleG)
57 (not (route-exists? 'F 'D SimpleG))
58 (route-exists? 'D 'C SimpleG)