;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-advanced-reader.ss" "lang")((modname week9_m_full) (read-case-sensitive #t) (teachpacks ((lib "image.ss" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "image.ss" "teachpack" "2htdp"))))) ; A ; / \ ; B C ; |\ / | ; D E F ; \ | / ; G ; arrows going down ; a graph is : ; (make-graph list-of-symbols (symbol -> list-of-symbols)) (define-struct graph (nodes neighbor)) (define mygraph (make-graph '(a b c d e f g) (lambda (node) (cond [(symbol=? node 'a) '(b c)] [(symbol=? node 'b) '(d e)] [(symbol=? node 'c) '(e f)] [(member node '(d e f)) '(g)] [(symbol=? node 'g) '()])))) (define mygraph2 (make-graph '(a b c d e f g) (lambda (node) (cond [(symbol=? node 'a) '(b c)] [(symbol=? node 'b) '(d e)] [(symbol=? node 'c) '(e f g)] [(member node '(d e f)) '(g)] [(symbol=? node 'g) '()])))) ; add-edge : graph symbol symbol -> graph ; adds an edge to a given graph (define (add-edge g from to) (make-graph (graph-nodes g) (lambda (x) (cond [(symbol=? x from) (cons to ((graph-neighbor g) x))] [else ((graph-neighbor g) x)])))) (check-expect (graph=? (add-edge mygraph 'c 'g) mygraph2) true) ; graph=? : graph graph -> boolean ; returns whether two graphs are equal (define (graph=? g1 g2) (and (equal? (graph-nodes g1) (graph-nodes g2)) (all-true (map (lambda (node) (neighbors-equal g1 g2 node)) (graph-nodes g1))) )) (check-expect (graph=? mygraph mygraph) true) (check-expect (graph=? g1 g2) true) (check-expect (graph=? g2 g3) false) (define g1 (make-graph (list 'a 'b) (lambda (x) (cond[(symbol=? x 'a) '(b)] [else '()])))) (define g2 (make-graph (list 'a 'b) (lambda (x) (cond[(symbol=? x 'a) '(b)] [else '()])))) (define g3 (make-graph (list 'a 'b) (lambda (x) (cond[(symbol=? x 'b) '(a)] [else '()])))) ; wishlist: ; neighbors-equal ; all-true ; lists-same-elements ; neighbors-equal : graph graph node -> boolean ; returns whether graphs have same neighbors for given node (define (neighbors-equal g1 g2 a) (lists-same-elements ((graph-neighbor g1) a) ((graph-neighbor g2) a))) (check-expect (neighbors-equal g1 g2 'a) true) (check-expect (neighbors-equal g2 g3 'a) false) ; all-true : list-of-boolean -> boolean ;returns true if all booleans are true or list is empty, false otherwise (define (all-true lob) (cond [(empty? lob) true] [else (and (first lob) (all-true (rest lob)))] )) (check-expect (all-true (list true true)) true) (check-expect (all-true (list true false)) false) ; lists-same-elements : list-of-X list-of-X -> boolean ; returns whether the lists contain the same elements ignoring order ; and duplication (define (lists-same-elements a b) (and (a-contains-b a b) (a-contains-b b a) )) (check-expect (lists-same-elements (list 1 2) (list 2 1)) true) (check-expect (lists-same-elements (list 1 2) (list 2 1 3)) false) (check-expect (lists-same-elements (list 1 2 2 3) (list 3 2 1)) true) ; a-contains-b : list-of-X list-of-X -> boolean ; returns whether the first list contains all elements in the second, ; ignoring duplication (define (a-contains-b a b) (all-true (map (lambda (x) (member? x a)) b))) (check-expect (a-contains-b (list 1 2) (list 2 1)) true) (check-expect (a-contains-b (list 1 2) (list 2)) true) (check-expect (a-contains-b (list 1 2) (list 3)) false)