;; 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 lec15) (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)) ;; road-count : Pittsburgh-road num (num num -> num) -> num (define (road-count pr dev COMBINE) (cond [(empty? pr) dev] [(branch? pr) (COMBINE (road-count (branch-left pr) dev COMBINE) (road-count (branch-right pr) dev COMBINE))])) ;; count-dead-ends : Pittsburgh-road -> num (define (count-dead-ends pr) (road-count pr 1 +)) ;; max-road : Pittsburgh-road -> num (define (max-road pr) (local [(define (max-plus-one l r) (+ 1 (max l r)))] (road-count pr 0 max-plus-one))) ;; We arrived at the above functions by abstracting over ;; these two: ;; ;;;;; count-dead-ends : Pittsburgh-road -> num ;;;(define (count-dead-ends pr) ;;; (cond ;;; [(empty? pr) 1] ;;; [(branch? pr) ;;; (+ (count-dead-ends (branch-left pr)) ;;; (count-dead-ends (branch-right pr)))])) ;;;;; max-road : Pittsburgh-road -> num ;;;(define (max-road pr) ;;; (cond ;;; [(empty? pr) 0] ;;; [(branch? pr) ;;; (+ 1 ;;; (max (max-road (branch-left pr)) ;;; (max-road (branch-right pr))))])) (define short-road (make-branch empty empty 'left)) (define medium-road (make-branch (make-branch empty empty 'right) empty 'left)) (check-expect (count-dead-ends empty) 1) (check-expect (count-dead-ends short-road) 2) (check-expect (count-dead-ends medium-road) 3) (check-expect (max-road empty) 0) (check-expect (max-road short-road) 1) (check-expect (max-road medium-road) 2)