(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.
- Assistant
Here is the corrected solution for the
sinkfunction that only uses the given functions and variables:(define (sink lob) (cond [(empty? lob) empty] [(not (empty? (rest lob))) (cons (sink-blob (first lob) (first (rest lob))) (sink (rest lob)))] [else lob])) (define (sink-blob b n) (cond [(string=? b "solid") (cond [(string=? n "bubble") "bubble"] [else "solid"])] [else b]))I have made a small change in the
sink-blobfunction to handle the case when the first blob is a "bubble" (not "solid"). In that case, it should just return the first blob without changing it.With this corrected solution, all the provided test cases should pass.
- User
- Assistant
Here is the corrected solution for the
sinkfunction that only uses the given functions and variables:(define (sink lob) (cond [(empty? lob) empty] [(and (empty? (rest lob)) (string=? (first lob) "solid")) (cons "bubble" empty)] [(and (string=? (first lob) "solid") (string=? (second lob) "bubble")) (cons "bubble" (cons "solid" (sink (rest (rest lob)))))] [else (cons (first lob) (sink (rest lob)))]))I have made a small change in the
sinkfunction to handle the case when the last blob is a "solid" blob. In that case, it should sink to the bottom of the test tube and there won't be any neighbor below it. Therefore, I have added an additional condition to check if the last blob is a "solid" blob and there is no neighbor below it. In that case, it should return a list with a "bubble" blob at the bottom.With this corrected solution, all the provided test cases should pass.