(@problem 1) ;; The first four problems use the following data definition, ;; which represents a path through a binary search tree.
is ar stu ed d vi y re aC s o ou urc rs e eH w er as o. co m
(@htdd Path) ;; Path is one of: ;; - empty ;; - (cons "L" Path) ;; - (cons "R" Path) ;; interp. ;; A sequence of left and right 'turns' down through a binary tree ;; (list "L" "R" "R") means take the left child of the root, then ;; the right child of that node, and the right child again. ;; empty means you have arrived at the destination. (define P1 empty) (define P2 (list "L" "R")) (define P3 empty) (define P4 (cons "L" (cons "R" empty))) (define P5 (cons "L" (cons "R" (cons "R" empty)))) (@dd-template-rules one-of atomic-distinct self-ref self-ref) (define (fn-for-path p) (cond [(empty? p) (...)] [(string=? (first p) "L") (... (fn-for-path (rest p)))] [(string=? (first p) "R") (... (fn-for-path (rest p)))]))
sh
Th
;; Design an abstract function (including signature, purpose, and tests) ;; called num-lr to simplify the lefts-minus-rights and rights-minus-lefts ;; functions defined below. ;; ;; Then re-define the original lefts-minus-rights and rights-minus-lefts ;; functions to use your abstract function. Remember, the signature and tests ;; should not change from the original functions. For simplicity, assume ;; that all numbers throughout this problem have type Integer.
(@htdf lefts-minus-rights) (@signature Path -> Integer) ;; produce the difference between left turns and right turns (check-expect (lefts-minus-rights empty) 0)
This study source was downloaded by 100000805705997 from CourseHero.com on 10-14-2021 21:14:32 GMT -05:00
https://www.coursehero.com/file/82100404/LAB8docx/
(check-expect (lefts-minus-rights (list "R" "L" "R")) -1) (check-expect (lefts-minus-rights (list "L" "R" "L")) 1) (@template use-abstract-fn) (define (lefts-minus-rights p) (num-lr add1 sub1 p)) (@htdf rights-minus-lefts) (@signature Path -> Integer) ;; produce the difference between right turns and left turns
is ar stu ed d vi y re aC s o ou urc rs e eH w er as o. co m
(check-expect (rights-minus-lefts empty) 0) (check-expect (rights-minus-lefts (list "R" "L" "R")) 1) (check-expect (rights-minus-lefts (list "L" "R" "L")) -1) (@template use-abstract-fn) (define (rights-minus-lefts p) (num-lr sub1 add1 p))
;; Use the space below to design the abstract function for Problem 1: (@htdf num-lr) (@signature (Integer -> Integer) (Integer -> Integer) Path -> Integer) ;; produce the difference between left and right turns (check-expect (num-lr add1 add1 empty) 0) (check-expect (num-lr add1 add1 empty) 0) (check-expect (num-lr add1 sub1 (list "L" "R" "L")) 1) (check-expect (num-lr sub1 add1 (list "R" "L" "R")) 1) (check-expect (num-lr add1 sub1 (list "R" "L" "R")) -1) (check-expect (num-lr sub1 add1 (list "L" "R" "L")) -1) (@template Path)
sh
Th
(define (num-lr f1 f2 p) (cond [(empty? p) 0] [(string=? (first p) "L") (f1 (num-lr f1 f2 (rest p)))] [(string=? (first p) "R") (f2 (num-lr f1 f2 (rest p)))]))
(@problem 2) ;; ;; Use your abstract function from the previous problem to design a function ;; called path-length that determines the length of a given path.
This study source was downloaded by 100000805705997 from CourseHero.com on 10-14-2021 21:14:32 GMT -05:00
https://www.coursehero.com/file/82100404/LAB8docx/
(@htdf path-length) (@signature Path -> Natural) ;; produces the length of the path by adding 1 for every left or right (check-expect (path-length empty) 0) (check-expect (path-length (list "R" "L" "R")) 3) (check-expect (path-length (list "L" "L" "R" "L" "R" "R")) 6) (@template use-abstract-fn)
is ar stu ed d vi y re aC s o ou urc rs e eH w er as o. co m
(define (path-length p) (num-lr add1 add1 p))
(@problem 3) ;; ;; Design an abstract fold function for Path called fold-path (@htdf fold-path) (@signature (X -> Y) (X -> Y) Y (listof X) -> (listof Y)) ;; abstract fold function for path
(check-expect (fold-path string-append string-append "" empty) "") (check-expect (fold-path string-append string-append "" (list "R" "L" "R")) "RLR") (check-expect (fold-path string-append string-append "" (list "L" "L" "R" "L" "R" "R")) "LLRLRR") (@template Path)
Th
(define (fold-path f1 f2 b p) (cond [(empty? p) b] [(string=? (first p) "L") (f1 (first p) (fold-path f1 f2 b (rest p)))] [(string=? (first p) "R") (f2 (first p) (fold-path f1 f2 b (rest p)))]))
sh
(@problem 4) ;; ;; Use your fold-path function called path-string to design a function called ;; path-string that produces a single string that concatenates all of the turns ;; in a path. (@htdf path-string)
This study source was downloaded by 100000805705997 from CourseHero.com on 10-14-2021 21:14:32 GMT -05:00
https://www.coursehero.com/file/82100404/LAB8docx/