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

  1. Sign in to GitHub.

  2. 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.

  3. 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.

  4. 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.

  5. 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

  1. Design the function swap that consumes a list of symbols los and two symbols s1 and s2. The function returns a list lst with all occurrences of s1 replaced with s2 and all occurrences of s2 replaced with s1, e.g.,
     (swap 'a 'd (list a b c d)) => (list d b c a) 
  2. Design the function product, that consumes sos1 and sos2 each a list of symbols without repetitions, returns a list of 2-lists that represents the Cartesian product of sos1 and sos2. 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))
  3. Design the function flatten that consumes a list of lists of numbers LLoN 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
  1. sum-numbers consumes an SEXP and returns the summation of all elements in SEXP that are Numbers.
  2. replace consumes an SEXP and two ATOMs a1 and a2 and replaces all occurrences of a1 in SEXP with a2. 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
    
  1. Provide a data definition for a list of bullets.
  2. Provide a data definition for a list of houses.
  3. 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 speed 4, direction 'Up and location (10,10) it gets updated to have speed 4, direction 'Up and location (10,6).
  4. 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,
      1. then the house's health decreases by the same amount as the bullets damage
      2. 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.
The team has decided that the game is going to be a two dimensional game and would like to represent the houses as a matrix. To do so the team has provided a new data definition
 ;; 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.