CSci 1301: Lab 4

Due: Friday, October 24th at 11:59pm by e-mail

What to submit

The lab is done in groups of 2. In the beginning of each file please write (in comments) the names fo all group members.

At the end of the lab please send me and your group partner(s) all your Racket files as e-mail attachments. My e-mail is elenam at morris.umn.edu. The subject of your e-mail must be "1301 Lab 4" followed by "Final" or "Not final", depending on whether this is a final submission or you are still working on it. If you need to finish it, make sure to set up a time with your group partner(s) to finish the lab.

Task 1 (5 points)

Write a function append-all-strings that, given a list of strings, creates a one long string out of them by appending them togeher. For instance, the following check-expect tests should pass:


(check-expect (append-all-strings (list "Hello " "there, " "stranger!")) "Hello there, stranger!")
(check-expect (append-all-strings empty) "") 

Task 2 (5 points)

Write a function select-short-strings that, given a list of strings and a number, creates a list of strings with length less than or equal to the given number. Start by writing check-expect tests for this function.

Task 3 (6 points)

Write a recursive function combine-images that takes a list of images and creates a combined image that is the result of putting all the images on the list besides each other (left-to-right). Note that Racket provides empty-image which is an image of heighta nd width 0 and can be used as a the base case, i.e. the result of the call of the function on an empty list. Below are some check-expect test. Note that the empty image in the first test doesn't make any difference in the result, but helps you understand how the result is formed.


(check-expect (combine-images (list (circle 10 "solid" "red") 
                                    (rectangle 20 30 "solid" "blue")))
              (beside (circle 10 "solid" "red") 
                      (beside (rectangle 20 30 "solid" "blue") 
                               empty-image)))
(check-expect (combine-images empty) empty-image)

Task 4 (15 points): Using a list as a world state.

Your goal is to develop a world program whose state is a list of positions of dots in a scene. The show function displays dots (all of the same size) at all the positions. Note that show must be recursive. See its description in the starting code below for more details.

When the user click on a position on the screen, it is added to the state, so show will make a dot appear there.

Once you have implemented this functionality, add handling a space bar so that it erases the last dot that's been added. Pressing the space bar N times will erase the last N dots. If there are no dots left, the scene must be left empty (there should be no error).

Start with this code. Write recursive show, it should show two dots given as the initial world state. Once this is done and tested, start adding the other required functionality.



;; the world state is the list of position of dots

; constants
(define width 200)
(define height 200)
(define half-w (/ width 2))
(define half-h (/ height 2))
(define dot (circle 10 "solid" "magenta"))
(define blank-scene (empty-scene width height))

(define (main duration)
  (big-bang (list (make-posn 10 50) (make-posn 150 60))
            [to-draw show] 
            ;; add handling of clicks and keys here 
            ))

;; show: list of positions -> image
;; a recursive functions that takes a list of posn structures and 
;; returns a scene with dots at each of these positions.
;; Returns an empty scene for an empty list. 

;; fill in the function here 

;; do-nothing: world state -> world state
;; takes the state of the world and returns it unchanged,
;; for world programs that don't change the world on clock ticks
(define (do-nothing ws) ws)

Extra credit: add another key handler: when the e is pressed, erase the "oldest" dot in the list, rather than the most recently added.


CSci 1301 course web site.