(require 2htdp/image) ;; SPD2-Design-Quiz-1.rkt ;;…
- User
(require 2htdp/image) ;; SPD2-Design-Quiz-1.rkt
;; ====================================================================== ;; Constants (define COOKIES .)
;; ====================================================================== ;; Data Definitions
;; Natural is one of: ;; - 0 ;; - (add1 Natural) ;; interp. a natural number (define N0 0) ;0 (define N1 (add1 N0)) ;1 (define N2 (add1 N1)) ;2
#; (define (fn-for-natural n) (cond [(zero? n) (…)] [else (… n ; n is added because it’s often useful (fn-for-natural (sub1 n)))]))
;; Template rules used: ;; - one-of: two cases ;; - atomic distinct: 0 ;; - compound: 2 fields ;; - self-reference: (sub1 n) is Natural
; PROBLEM 1: ; ; Complete the design of a function called pyramid that takes a natural ; number n and an image, and constructs an n-tall, n-wide pyramid of ; copies of that image. ; ; For instance, a 3-wide pyramid of cookies would look like this: ; ; .
;; Natural Image -> Image ;; produce an n-wide pyramid of the given image (check-expect (pyramid 0 COOKIES) empty-image) (check-expect (pyramid 1 COOKIES) COOKIES) (check-expect (pyramid 3 COOKIES) (above COOKIES (beside COOKIES COOKIES) (beside COOKIES COOKIES COOKIES)))
;(define (pyramid n i) empty-image) ; stub
(define (pyramid n img) (cond [(zero? n) empty-image] [else (above (pyramid (sub1 n) img) (pyramid-row n img))]))
(define (pyramid-row n img) (cond [(zero? n) empty-image] [else (beside img (pyramid-row (sub1 n) img))]))
; Problem 2: ; 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. Let’s capture this idea in BSL. ; ; 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 ; neighbor just below it. ; ; To assist you, we supply the relevant data definitions.
;; 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
- Assistant
(define (sink lob) (cond [(empty? lob) empty] [else (cons (sink-blob (first lob)) (sink (rest lob)))]))
(define (sink-blob b) (if (string=? b "solid") "bubble" b))
- User
- Assistant
It seems that the
sinkfunction is not correctly sinking the solid blobs. The problem lies in thesink-blobhelper function. Currently, it checks if the blob is equal to "solid", and if so, it replaces it with "bubble". However, this only sinks the bottommost solid blob but not the ones above it.To fix this, we need to modify the
sink-blobfunction to check if the blob is equal to "solid" and if it has "bubble" below it. If both conditions are met, we replace the blob with "bubble". Otherwise, we leave it unchanged. Here's the updated code:(define (sink-blob b) (if (and (string=? b "solid") (eq? (second lob) "bubble")) "bubble" b))With this modification, the
sinkfunction should now correctly sink all the solid blobs in the test tube. - User
- Assistant
After carefully reviewing the code, I noticed that I made a mistake. In the
sink-blobfunction, instead of checking if the blob has a "bubble" below it, I mistakenly checked if the second element of the listlobis equal to "bubble". However,lobdoes not exist in the context of the helper function. To fix this, I need to modify thesink-blobfunction to check if the blob has a "bubble" below it.To do that, we can use pattern matching to destructure the list of blobs and check if the pattern
('solid "bubble" _)matches the input list. Here's the updated code:(define (sink-blob b) (match b [('solid "bubble" _) "bubble"] [