Assignment 7
Due Date : 11/6 @ 11:59pm
Instructions
Each of you should have a repository in GitHub with the name
assignment7-githubHandle
. Use this repository
and add all your work there. In order to clone the repositories
from GitHub to your machine (or watch this video video
instructions):
-
Sign in to GitHub.
-
On your GitHub home page your Github Handle should appear on the left as a button/drop down menu. Click that button and from the drop down menu select
cs2500f14
. The GitHub page will then navigate to the class' web page and to the contents that is available to you. -
On the right you should be able to see all repositories to which you have been given access. Find the repository whose name matches the pattern
assignment7-githubHandle
, where githubHandle is your GitHub account name, e.g,assignment7-john123
, and click on it. -
On the repositories home page look at the bottom right hand corner for a button with the text Clone in Desktop. By clicking on this button your browser will launch the GitHub client that we installed in class and ask for a location on your drive to store the repository.
If you are not using the GitHub client the clone URL is located above the Clone in Desktop button. Copy the URL and issue the following command on your shell
git clone URL
. - Create a file and save it under the folder on your drive that you selected in the preceding step.
Remember to push your changes to the GitHub repository often.
Note
For each question that asks you to design a program you are
expected to follow the Design Recipe in the same way as we did
in class. Failure to show all your work for each step
will cost you points. All tests should use check-expect
.
Problem 1
- Design the function
swap
that consumes a list of symbolslos
and two symbolss1
ands2
. The function returns a listlst
with all occurrences ofs1
replaced withs2
and all occurrences ofs2
replaced withs1
, e.g.,(swap 'a 'd (list a b c d)) => (list d b c a)
- Design the function
product
, that consumessos1
andsos2
each a list of symbols without repetitions, returns a list of 2-lists that represents the Cartesian product ofsos1
andsos2
. The 2-lists may appear in any order.(product (list a b c) (list x y)) => (list (list a x) (list a y) (list b x) (list b y) (list c x) (list c y))
- Design the function
flatten
that consumes a list of lists of numbersLLoN
and returns a single list that contains all the numbers (put differently remove the inner lists).(flatten (list (list 1 2) (list 4 5) (list 10 19))) => (list 1 2 4 5 10 19)
Problem 2
Given the following data definition
;; An ATOM is one of: ;; -- Symbol ;; -- String ;; -- Number ;; An SEXP (S-expression) is one of: ;; -- empty ;; -- (cons ATOM SEXP) ;; -- (cons SEXP SEXP)Design the following functions
-
sum-numbers
consumes anSEXP
and returns the summation of all elements inSEXP
that are Numbers. -
replace
consumes anSEXP
and twoATOM
sa1
anda2
and replaces all occurrences ofa1
inSEXP
witha2
. For example,(replace (list (list 'a 'b) (list 1 3 )) 'a 'b) => (list (list 'b 'b) (list 1 3)) (replace (list (list 'a 'b) (list 1 3)) 1 2) => (list (list 'a 'b) (list 2 3))
Problem 3
You are part of a team that is working on a game. The game consists of houses on the ground and bullets being fired at the houses. The team has decided on the following data definitions.
;; A House is (make-house Posn Number) ;; interpretation: represents a house's position and ;; health (define-struct house (location health)) ;; A Bullet is (make-bullet Posn Number Direction Number) ;; interpetation: represents a bullet, its current position, ;; its speed, its direction and its potential damage (define-struct bullet (location speed direction damage)) ;; A Direction is one of ;; - 'Up ;; - 'Down ;; - 'Left ;; - 'Right
- Provide a data definition for a list of bullets.
- Provide a data definition for a list of houses.
-
Provide a function called
move-bullets
that takes in a list of bullets and updates each bullet's location based on the bullets direction and speed. For example if a bullet has speed4
, direction'Up
and location(10,10)
it gets updated to have speed4
, direction'Up
and location(10,6)
. -
Provide a function
update-houses
that consumes a list of houses and a list of bullets and returns a list of updated houses. The updates have the following behaviour-
if a bullet has the same location as a house,
- then the house's health decreases by the same amount as the bullets damage
- If the house (after being hit by a bullet) has its health as 0 or less than 0 it is removed from the list.
- else the house remains intact.
-
if a bullet has the same location as a house,
;; A Village is one of ;; - empty ;; - (cons LoH Village) ;; An LoH is a list of houses (provided by part 2 of this question)
Provide a new implementation of update-houses
, call it update-village
that will take as input a list of bullets and a village and return an updated village. The updates have the same behaviour as before.