Question
Consider a test tube filled with solid blobs and bubbles. Over time, the solids sink to the bottom of the test tube and as a
Consider a test tube filled with solid blobs and bubbles. Over time, the solids sink to the bottom of the test tube and as a consequence, the bubbles percolate to the top.
Given the data definitions to represent a list of blobs, complete the design of a function that takes a list of blobs and sinks each solid blob by one. It's okay to assume that a solid blob sinks past any neighbour just below it.
Think carefully about the helper rules when you are designing your sink function. You will need to design helper functions.
Remember that the check-expects serve as examples first. If you are unsure about how your sink function should work, write more examples and have your TA check them. You may find examples of length one and two helpful.
;; Blob is one of: ;; - "solid" ;; - "bubble" ;; interp. a gelatinous blob, either a solid or a bubble ;; Examples are redundant for enumerations #; (define (fn-for-blob b) (cond [(string=? b "solid") (...)] [(string=? b "bubble") (...)]))
;; Template rules used: ;; - one-of: 2 cases ;; - atomic distinct: "solid" ;; - atomic distinct: "bubble"
;; ListOfBlob is one of: ;; - empty ;; - (cons Blob ListOfBlob) ;; interp. a sequence of blobs in a test tube, listed from top to bottom. (define LOB0 empty) ; empty test tube (define LOB2 (cons "solid" (cons "bubble" empty))) ; solid blob above a bubble
#; (define (fn-for-lob lob) (cond [(empty? lob) (...)] [else (... (fn-for-blob (first lob)) (fn-for-lob (rest lob)))]))
;; Template rules used ;; - one-of: 2 cases ;; - atomic distinct: empty ;; - compound: 2 fields ;; - reference: (first lob) is Blob ;; - self-reference: (rest lob) is ListOfBlob
;; ========
;; ListOfBlob -> ListOfBlob ;; produce a list of blobs that sinks the given solid blobs by one
(check-expect (sink empty) empty) (check-expect (sink (cons "bubble" (cons "solid" (cons "bubble" empty)))) (cons "bubble" (cons "bubble" (cons "solid" empty)))) (check-expect (sink (cons "solid" (cons "solid" (cons "bubble" empty)))) (cons "bubble" (cons "solid" (cons "solid" empty)))) (check-expect (sink (cons "solid" (cons "bubble" (cons "bubble" empty)))) (cons "bubble" (cons "solid" (cons "bubble" empty))))
; ;PRELAB: Complete these three check-expects, then uncomment them. ; ; (check-expect (sink (cons "solid" (cons "bubble" (cons "solid" empty)))) ; ...) ; ; (check-expect (sink (cons "bubble" (cons "solid" (cons "solid" empty)))) ; ...) ; ; (check-expect (sink (cons "solid" ; (cons "solid" ; (cons "bubble" (cons "bubble" empty))))) ; ...) ;
(define (sink lob) empty) ; stub
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started