;; relative->absolute : [Listof Number] -> [Listof Number] ;; converts relative distances to absolute distance from start point ;; e.g. convert '(1 2 2 2 1) to '(1 3 5 7 8) (define (relative->absolute lor0) (local (;; acc represents distance already covered (define (rel->abs lor acc) (cond [(empty? lor) empty] [else (local ((define fst (first lor)) (define newacc (+ acc fst))) (cons newacc (rel->abs (rest lor) newacc)))]))) (rel->abs lor0 0))) #;(define (relative->absolute lor) (cond [(empty? lor) empty] [(cons? lor) (cons (first lor) (map (lambda (x) (+ x (first lor))) (relative->absolute (rest lor))))])) (check-expect (relative->absolute '()) '()) (check-expect (relative->absolute '(3)) '(3)) (check-expect (relative->absolute '(1 2 2 2 1)) '(1 3 5 7 8)) ;; my-foldl : [X Y -> Y] Y [Listof X] -> Y ;; base serves as accumulator; represents answer so far ;; Idea: (foldl f base (cons x xs)) = (foldl f (f x base) xs) (define (my-foldl f base lox) (cond [(empty? lox) base] [(cons? lox) (my-foldl f (f (first lox) base) (rest lox)) ])) (check-expect (my-foldl cons empty '(a b c d e)) '(e d c b a)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-struct node (toll left right)) ;; A BT is one of: ;; - 'leaf ;; - (make-node Number BT BT) ;; min-toll : BT -> Number ;; computer the minimum toll you must pay to get from the root of bt ;; to any leaf (define (min-toll bt) ...) (define t1 (make-node 4 'leaf 'leaf)) (define t2 (make-node 9 'leaf t1)) (define t3 (make-node 9 t1 t1)) (define t4 (make-node 3 t3 t2)) ;(check-expect (min-toll 'leaf) 0) ;(check-expect (min-toll t1) 4) ;(check-expect (min-toll t2) 9) ;(check-expect (min-toll t3) 13)