Thanks for rating. Please let me know if you need more help
Seller
Follow
solutions
Reviews received
Content preview
(@problem 1)
;; The first four problems use the following data definition,
;; which represents a path through a binary search tree.
(@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.
m
er as
(define P1 empty)
(define P2 (list "L" "R"))
co
eH w
(define P3 empty)
(define P4 (cons "L" (cons "R" empty)))
o.
rs e
(define P5 (cons "L" (cons "R" (cons "R" empty))))
ou urc
(@dd-template-rules one-of atomic-distinct self-ref self-ref)
(define (fn-for-path p)
o
(cond [(empty? p) (...)]
aC s
[(string=? (first p) "L") (... (fn-for-path (rest p)))]
vi y re
[(string=? (first p) "R") (... (fn-for-path (rest p)))]))
ed d
;; Design an abstract function (including signature, purpose, and tests)
ar stu
;; called num-lr to simplify the lefts-minus-rights and rights-minus-lefts
;; functions defined below.
;;
is
;; Then re-define the original lefts-minus-rights and rights-minus-lefts
;; functions to use your abstract function. Remember, the signature and tests
Th
;; should not change from the original functions. For simplicity, assume
;; that all numbers throughout this problem have type Integer.
sh
(@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
m
er as
(@template use-abstract-fn)
(define (rights-minus-lefts p)
co
eH w
(num-lr sub1 add1 p))
o.
rs e
;; Use the space below to design the abstract function for Problem 1:
ou urc
(@htdf num-lr)
(@signature (Integer -> Integer) (Integer -> Integer) Path -> Integer)
o
;; produce the difference between left and right turns
aC s
vi y re
(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)))]))
sh
(@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
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller solutions. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $3.49. You're not tied to anything after your purchase.