;; 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-lambda-reader.ss" "lang")((modname lec18) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ;; is-sequence? : list-of-num -> boolean? (define (is-sequence? l) (is-sequence-starting-from? l 0)) ;; is-sequence? : list-of-num num -> boolean? ;; n accumulateds how many items we have seen (define (is-sequence-starting-from? l n) (cond [(empty? l) true] [(cons? l) (and (= n (first l)) (is-sequence-starting-from? (rest l) (add1 n)))])) (check-expect (is-sequence-starting-from? empty 5) true) (check-expect (is-sequence-starting-from? '(1 2 3) 0) false) (check-expect (is-sequence-starting-from? '(1 2 3) 1) true) (check-expect (is-sequence? empty) true) (check-expect (is-sequence? '(0 1 2 3)) true) (check-expect (is-sequence? '(1 2 3)) false) (check-expect (is-sequence? '(0 1 2 4)) false) (check-expect (is-sequence? '(0 1 2 1)) false) ;; -------------------------------------------------- ;; A maze ;; - num ;; - (make-branch num maze maze) (define-struct branch (spells left right)) ;; Score is total number of spells seen at branches ;; times amount of gold at end (define always-lose 10) (define always-four (make-branch 2 2 2)) (define sometimes-four (make-branch 1 4 0)) (define bigger (make-branch 5 always-four sometimes-four)) ;; max-score : maze num -> num (define (max-score-for-spells m num-spells) (cond [(number? m) (* m num-spells)] [(branch? m) (local [(define new-num-spells (+ num-spells (branch-spells m)))] (max (max-score-for-spells (branch-left m) new-num-spells) (max-score-for-spells (branch-right m) new-num-spells)))])) ;; max-score : maze -> num (define (max-score m) (max-score-for-spells m 0)) (check-expect (max-score always-lose) 0) (check-expect (max-score always-four) 4) (check-expect (max-score sometimes-four) 4) (check-expect (max-score bigger) 24)