Skip to main content

University of British Columbia CPSC 110 Lab 5

Page 1

;; CPSC 110 - Helpers Lab

(require spd/tags) (require 2htdp/image) (require 2htdp/universe)

(@assignment lab-05) (@cwl ???)

;; 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:

;(bitmap/url ;"https://edx-course-spdx-kiczales.s3.amazonaws.com/HTC/lab/ ; cookies-pyramid.png") ;(run in interactions window)

; Constants

(define COOKIES (bitmap/url "https://edx-course-spdx-kiczales.s3.amazonaws.com/HTC/lab/cookies.png")) (define SQUARE (square 10 "solid" "blue"))


;; ====================================================================== ;; Data Definitions

(@htdd Natural) ;; 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

(@dd-template-rules one-of atomic-distinct compound self-ref)

(define (fn-for-natural n) (cond [(zero? n) (...)] [else (... n ; n is added because it's often useful (fn-for-natural (sub1 n)))]))

;; ========== ;; Functions


(@problem 1) (@htdf pyramid) (@signature Natural Image -> Image) ;; produce an n-tall, n-wide pyramid of the given image (check-expect (pyramid 0 COOKIES) empty-image) (check-expect (pyramid 1 SQUARE) SQUARE) (check-expect (pyramid 3 COOKIES) (above COOKIES (beside COOKIES COOKIES) (beside COOKIES COOKIES COOKIES)))

;(define (pyramid n i) empty-image) ; stub

(@template Natural)

(define (pyramid n i) (cond [(zero? n) empty-image] [else (above (pyramid (sub1 n) i) (layout-images n i))]))

(@htdf layout-images) (@signature Natural Image -> Image) ;; produce the given number of Images beside each other (check-expect (layout-images 0 empty-image) empty-image) (check-expect (layout-images 3 COOKIES)


(beside COOKIES COOKIES COOKIES)) (check-expect (layout-images 5 SQUARE) (beside SQUARE SQUARE SQUARE SQUARE SQUARE))

;(define (layout-images n i) empty-image) ;stub

(@template Natural)

(define (layout-images n i) (cond [(zero? n) empty-image] [else (beside i (layout-images (sub1 n) i))]))

;; 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. ;; ;; Complete the design of a world program that displays the test tube of blobs. ;; Initially, the test tube is empty, but we can add solid blobs and bubbles by ;; pressing the "s" and "b" keys respectively. Pressing the spacebar will sink ;; each solid blob by one. ;; ;; We have completed most of this program already. All you will need to do is ;; complete the sink function (a helper function for handle-key). This function


;; will consume a list of blobs and sinks each solid blob by one. You can ;; assume that a solid blob will sink past any neighbour just below it.

;; CONSTANTS ============================

(define WIDTH 300) (define HEIGHT 500)

(define TUBE-WIDTH 50) (define TUBE (overlay/align/offset "middle" "bottom" (rectangle TUBE-WIDTH (- HEIGHT TUBE-WIDTH) "solid" "silver") 0 (/ TUBE-WIDTH 2) (circle (/ TUBE-WIDTH 2) "solid" "silver")))

(define MTS (overlay/align "middle" "bottom" TUBE (empty-scene WIDTH HEIGHT)))

(define X-CTR (/ WIDTH 2))

(define BLOB-RADIUS (* 0.4 TUBE-WIDTH)) (define SOLID (circle BLOB-RADIUS "solid" "black")) (define BUBBLE (circle BLOB-RADIUS "outline" "blue"))


Turn static files into dynamic content formats.

Create a flipbook
University of British Columbia CPSC 110 Lab 5 by AnswerDone - Issuu