CPSC 110 LAB 1 HtDF Lab P3 A (much too) simple scheme for pluralizing words in English is to add an s at the end unless the word already ends in s. Design a function that consumes a string, and adds s to the end unless the string already ends in s.
is ar stu ed d vi y re aC s o ou urc rs e eH w er as o. co m
;; String -> String ;; Adds an s to end of given word, unless word already ends in s (check-expect (pluralize "hello") "hellos") (check-expect (pluralize "has") "has") (check-expect (pluralize "") "") ;(define (pluralize str) "") ;stub ;(define (pluralize str) ; (... str)) ;template
(define (pluralize str) (if (string=? str "") "" (if (string=? "s" (string-ith str (- (string-length str) 1))) str (string-append str "s"))))
P4 Design a function called nth-char-equal? that consumes two strings and a natural and produces true if the strings both have length greater than n and have the same character at position n. Note, the signature for such a function is: ;; String String Natural -> Boolean
sh
Th
The template for such a function is: (define (nth-char-equal? s1 s2 n) (... s1 s2 n))
;; String String Natural -> Boolean ;; Produces true if string-length of s1 and s2 are greater than n and have the same character at position n (check-expect (nth-char-equal? "hello" "goodbye" 10) false) (check-expect (nth-char-equal? "hello" "bell" 6) false) (check-expect (nth-char-equal? "time" "bath" 3) false) (check-expect (nth-char-equal? "candy" "mandy" 3) true)
This study source was downloaded by 100000805705997 from CourseHero.com on 10-14-2021 20:45:30 GMT -05:00
https://www.coursehero.com/file/46578028/CPSC-110-LAB-1docx/
;(define (nth-char-equal? s1 s2 n) false) ;stub ;(define (nth-char-equal? s1 s2 n) ; (... s1 s2 n)) ;template (define (nth-char-equal? s1 s2 n) (if (> (string-length s1) n) (if (> (string-length s2) n) (string=? (substring s1 (- n 1) n) (substring s2 (- n 1) n)) false) false))
is ar stu ed d vi y re aC s o ou urc rs e eH w er as o. co m
Balloon Lab ; Balloon popping
;; CONSTANTS ========================== (define WIDTH 500) (define HEIGHT 500) (define MTS (empty-scene WIDTH HEIGHT))
(define BALLOON-COLOUR "red") (define POP-IMAGE (overlay (text "POP!" 80 "black") (radial-star 30 (/ WIDTH 10) (/ WIDTH 2) "solid" "yellow"))) (define CTR-X (/ WIDTH 2)) (define CTR-Y (/ HEIGHT 2)) (define SPEED 2)
(define MAX-SIZE (/ WIDTH 2))
;; DATA DEFINITIONS ============================
sh
Th
;; Balloon is one of: ;; - Number[1, MAX-SIZE] ;; - false ;; interp. number is the radius of balloon in screen pixels, false means balloon is popped (define B1 1) (define B2 false) (define B3 10) (define B4 MAX-SIZE) (define (fn-for-balloon b) (cond [(and (number? b)
This study source was downloaded by 100000805705997 from CourseHero.com on 10-14-2021 20:45:30 GMT -05:00
https://www.coursehero.com/file/46578028/CPSC-110-LAB-1docx/
(>= b 1) (< b MAX-SIZE)) (... b)] [else b])) ;template ;; Template rules used: ;; - One of: 2 cases ;; - atomic non-distinct: Number[1, MAX-SIZE] ;; - atomic distinct: false
;; FUNCTIONS ====================================
is ar stu ed d vi y re aC s o ou urc rs e eH w er as o. co m
;; Balloon -> Balloon ;; starts the world program with (main 1) ; no examples for main function
(define (main b) (big-bang b (on-tick next-balloon) ;Balloon->Balloon (to-draw render) ;Balloon->Image (on-key handle-key))) ;Balloon KeyEvent -> Balloon
;; Balloon -> Balloon ;; expands the balloon by SPEED, until it is popped (check-expect (next-balloon 1) (+ 1 SPEED)) (check-expect (next-balloon 10) (+ 10 SPEED)) (check-expect (next-balloon MAX-SIZE) false) ;(define (next-balloon b) b) ;stub ;<template taken from Balloon>
sh
Th
(define (next-balloon b) (cond [(and (number? b) (>= b 1) (< b MAX-SIZE)) (+ b SPEED)] [else false]))
;; Balloon -> Image ;; draws the balloon of given size onto the MTS, unless popped (check-expect (render 1) (place-image (circle 1 "solid" "red") CTR-X CTR-Y MTS)) (check-expect (render 10) (place-image (circle 10 "solid" "red") CTR-X CTR-Y MTS)) (check-expect (render MAX-SIZE) (place-image POP-IMAGE CTR-X CTR-Y MTS)) (check-expect (render false) (place-image POP-IMAGE CTR-X CTR-Y MTS))
This study source was downloaded by 100000805705997 from CourseHero.com on 10-14-2021 20:45:30 GMT -05:00
https://www.coursehero.com/file/46578028/CPSC-110-LAB-1docx/
;(define (render b) MTS) ;stub ;<template taken from Balloon> (define (render b) (cond [(and (number? b) (>= b 1) (< b MAX-SIZE)) (place-image (circle b "solid" "red") CTR-X CTR-Y MTS)] [else (place-image POP-IMAGE CTR-X CTR-Y MTS)]))
is ar stu ed d vi y re aC s o ou urc rs e eH w er as o. co m
;; Balloon KeyEvent -> Balloon ;; pops balloon when space bar is pressed (check-expect (handle-key 1 " ") false) (check-expect (handle-key 10 " ") false) (check-expect (handle-key MAX-SIZE " ") false) (check-expect (handle-key 2 "a") 2) ;(define (handle-key b ke) b) ;stub (define (handle-key b ke) (cond [(key=? ke " ") false] [else b]))
Helper Lab
;; ListOfBlob -> ListOfBlob ;; produce a list of blobs that sinks the given solid blobs by one
Th
(check-expect (sink empty) empty) (check-expect (sink (cons "bubble" (cons "solid" (cons "bubble" empty)))) ;solid in middle (cons "bubble" (cons "bubble" (cons "solid" empty)))) (check-expect (sink (cons "solid" (cons "solid" (cons "bubble" empty)))) ;two solids on top (cons "bubble" (cons "solid" (cons "solid" empty)))) (check-expect (sink (cons "solid" (cons "bubble" (cons "bubble" empty)))) ;one solid on top (cons "bubble" (cons "solid" (cons "bubble" empty))))
sh
(check-expect (sink (cons "solid" (cons "bubble" (cons "solid" empty)))) ;solid on top and bottom (cons "bubble" (cons "solid" (cons "solid" empty)))) (check-expect (sink (cons "bubble" (cons "solid" (cons "solid" empty)))) ;two solids on bottom (cons "bubble" (cons "solid" (cons "solid" empty)))) (check-expect (sink (cons "solid" (cons "solid" (cons "bubble" (cons "bubble" empty))))) ;two solids on top, two bubbles on bottom (cons "bubble" (cons "solid" (cons "solid" (cons "bubble" empty)))))
This study source was downloaded by 100000805705997 from CourseHero.com on 10-14-2021 20:45:30 GMT -05:00
https://www.coursehero.com/file/46578028/CPSC-110-LAB-1docx/
;(define (sink lob) empty) ;stub ;<Template from ListOfBlob> (define (sink lob) (cond [(empty? lob) empty] [else (next-lob (first lob) (sink (rest lob)))]))
is ar stu ed d vi y re aC s o ou urc rs e eH w er as o. co m
;; Blob ListOfBlob -> ListOfBlob ;; if blob is "solid", move solid blob down list by one ;; ASSUME: (rest lob) is list where solid blobs have already moved down by one (check-expect (next-lob "bubble" (cons "solid" empty)) (cons "bubble" (cons "solid" empty))) (check-expect (next-lob "solid" (cons "solid" empty)) (cons "solid" (cons "solid" empty))) (check-expect (next-lob "solid" (cons "bubble" empty)) (cons "bubble" (cons "solid" empty))) (check-expect (next-lob "solid" (cons "bubble" (cons "bubble" empty))) (cons "bubble" (cons "solid" (cons "bubble" empty)))) (check-expect (next-lob "solid" (cons "solid" (cons "bubble" empty))) (cons "solid" (cons "solid" (cons "bubble" empty)))) ;(define (next-lob b lob) lob) ;stub
(define (next-lob b lob) (cond [(empty? lob) (cons b lob)] [else (if (solid? b) (cons (first lob) (cons "solid" (rest lob))) (cons "bubble" lob))]))
Th
;; Blob -> Boolean ;; produce true if blob is "solid" (check-expect (solid? "solid") true) (check-expect (solid? "bubble") false)
sh
;(define (solid? b) true) ;stub
(define (solid? b) (cond [(string=? b "solid") true] [(string=? b "bubble") false]))
------------ MIDTERM 1 -------------
This study source was downloaded by 100000805705997 from CourseHero.com on 10-14-2021 20:45:30 GMT -05:00
https://www.coursehero.com/file/46578028/CPSC-110-LAB-1docx/