;; 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-intermediate-reader.ss" "lang")((modname lec14) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ;; A Pittsburgh-road is either ;; - empty ;; - (make-branch Pittsburgh-road Pittsburgh-road sym) (define-struct branch (left right sign)) ;; metric : Pittsburgh-road num (num num -> num) -> num ;; To count all the dead ends in pr (define (metric pr base OP) (cond [(empty? pr) base] [(branch? pr) (OP (metric (branch-left pr) base OP) (metric (branch-right pr) base OP))])) ;; Original: ;;; count-ends : Pittsburgh-road -> num ;;; To count all the dead ends in pr ;(define (count-ends pr) ; (cond ; [(empty? pr) 1] ; [(branch? pr) (+ (count-ends (branch-left pr)) ; (count-ends (branch-right pr)))])) ;; count-ends : Pittsburgh-road -> num ;; To count all the dead ends in pr (define (count-ends pr) (metric pr 1 +)) (define big-pr (make-branch empty (make-branch empty (make-branch empty empty 'left) 'right) 'left)) (check-expect (count-ends empty) 1) (check-expect (count-ends (make-branch empty empty 'left)) 2) (check-expect (count-ends big-pr) 4) ;; Original: ;;; max-drive : Pittsburgh-road -> num ;;; Max number of steps to drive one-way to read a dead end ;(define (max-drive pr) ; (cond ; [(empty? pr) 0] ; [(branch? pr) (+ 1 (max (max-drive (branch-left pr)) ; (max-drive (branch-right pr))))])) ;; max-plus-one : num num -> num (define (max-plus-one a b) (+ 1 (max a b))) (check-expect (max-plus-one 3 5) 6) ;; max-drive : Pittsburgh-road -> num ;; Max number of steps to drive one-way to read a dead end (define (max-drive pr) (metric pr 0 max-plus-one)) (check-expect (max-drive empty) 0) (check-expect (max-drive (make-branch empty empty 'left)) 1) (check-expect (max-drive big-pr) 3)