Purpose The purpose of this lab is to give you some hands-on experience with list-processing functions and programs. When you walk out of this lab, you should understand a variety of list-based data definitions and how to design functions for these.
For those of you who programmed before, the lists you see here are so-called linked lists, equipped with basic functions (cons, first, rest, cons?, and empty?), which you could of course write in your favorite language. You also encounter empty, a special piece of data.
For everyone, when an interviewer asks in the future whether you know about linked data structures, the answer is yes, we encountered them in the third week in our first lecture. Conventional approaches to programming introduce such data structures at the end of the second semester or in the third one; you have a leg up here.
TAs: work through this part with your students.
Last night one of your TAs (who will remain nameless) was working on the BSL codebase and accidentally broke some functions and values related to lists. Since there hasn’t been time to fix it yet, we’ll have to create our own version of a list.
Sample Problem Now doubting your TAs’ mental capabilities, the professor has asked the TAs to design a function to check that they’ve correctly added points on the last homework. The poor TAs, crippled by fear, need your help!
Design a function that sums the elements in an arbitrarily long list of numbers.
Using the design recipe, we will break down the above question into smaller steps: data definition, data examples, templates, design of function.
Sample Problem Develop a data definion for hand-rolled lists of numbers. Here is the one we came up with.
(define-struct empty-lon ()) (define-struct cons-lon (left right)) ; A HRLoN (Hand-rolled List of Numbers) is one of: ; - (make-empty-lon) ; - (make-cons-lon Number HRLoN)Create three examples of HRLoNs
Structures without fields look strange but this is what is happening with make-empty-lon as well as the built in Racket version empty. The key idea is you can use empty-lon? to identify an empty-lon, which is unique piece of data in BSL.
Note The idea of using structure type definitions without fields shows how to apply the ideas of this course to object-oriented languages such as Java. There you define classes without fields for a similar purpose.
Sample Problem Develop a template for HRLoNs. Remember that the template follows from the data definition via four questions:
How many cases are there in the data definition?
What are the tests needed to distinguish them?
Do we need selectors in any of the cases?
Does the data definition involve self-references?
Sample Problem Now you are ready to design the function hrlon-sum. Remember to ask the following questions:
What kind of inputs does it consume?
What type of data does it output?
What does this output mean?
Exercise 2 Design the sum function. It should operate just like hrlon-sum but accept your new type of data as input rather than HRLoNs.
Exercise 3 Design the function average that consumes a list of numbers and returns the average of those numbers.
Exercise 4 Design the function overlay-all, which takes a list of images. It overlays each image onto the next image in the TA: explain how to read the documentation, especially the arrow notation for overlay. list. Giving the function an empty list should produce and empty scene of some size. You can choose how big it should be.
Hint Follow the design recipe, starting with with a data definition for a list of images.
Exercise 5 Design the function sum-all that consumes a list of lists of numbers and returns the sum of all the numbers from all of the lists.
A scientific simulation firm needs to simulate the effects of gravity on an arbitrarily large number of falling balls. Your job is to create a BSL simulation where the scientists can spawn new falling balls by clicking the mouse. They will fall to the ground for a bit until they go off the screen. The app should be in a 500 x 500 window. Once the balls go off screen the scientists no longer care about them.
(require 2htdp/image) (require 2htdp/universe) ; Ball -> LoB (define (main b) (big-bang (cons b empty) ; <— the world state is a LoB [to-draw draw-lob] [on-tick go] [on-mouse new-ball])) ; LoB -> LoB ; move balls, apply gravity, and then filter out those that are off screen (define (go lob) (on-screen-balls (apply-gravity (move-all lob))))
As you can see, the scientists kind of followed the design recipe for world programs. But you know how scientists are. So they leave it to you to finish the job.
Exercise 6 Develop a data definition for a Ball. A Ball should have a position in x and y and a velocity in x and y (pixels per clock tick).
Domain knowledge What is a velocity? What is a speed? If you don’t recall or if you have never heard, do not hesitate to ask the TA.
Exercise 7 Develop a data definition for a LoB (list of Balls).
Exercise 8 Develop templates for both data definitions. The template for LoB should refer to the template for Ball. Why?
Switch roles after each function you design so that you both get experience writing functions for lists.
Exercise 9 Design the function off-screen?, which determines whether a ball is off the screen.
Hint Use global constants for the size of the canvas rather than putting constant values directly into your code. We do this because you may need to use the canvas size values more than once.
Exercise 10 Design the function move-ball that consumes a Ball and creates a new one that has been moved according to its velocity. The new ball’s velocity is the same as the one used as input.
Domain knowledge If you do not recall what it means for an object to be located at (x,y) and to move by a velocity of (dx,dy), ask your TA.
Exercise 11 Design the function gravity that consumes a ball and creates a new one whose y velocity has been increased by gravitational acceleration.
Exercise 12 Design the function draw-lob that draws a list of balls on to an empty canvas.
Hint If you are using overlay instead of place-image then be sure to have a transparent background, by passing the "transparent" color into the empty-scene function. It can also be useful to first design the draw-ball function.
Exercise 13 Design the function on-screen-balls that filters out all that balls that are off of the screen from a list of balls.
Exercise 14 Design the function move-all that moves every ball in a list of balls according to its velocity.
Exercise 15 Design the function apply-gravity that applies the gravity function to every ball in a list of balls.
You can now comment out the on-mouse clause with #; to run the program.
Exercise 16 Design the function new-ball that adds a ball wherever the user clicks the mouse. The new ball will have a random velocity. You will give this function to on-mouse
Hint Use the function random.
If you finish early, try adding a color to your Ball structure. Change the drawing function to draw each ball with the correct color. Change the new-ball function to create a new ball of a random color.