Assignment 9a
Due Date: Monday Thursday at 9:00pm (Week 9)
Purpose To practice designing functions on nested lists of data, and refactoring earlier programs.
Exercise 1 Consider the following data definition:
(define-struct addition [op1 op2]) (define-struct subtraction [op1 op2]) (define-struct multiplication [op1 op2]) (define-struct division [op1 op2]) ; A MathProblem is one of: ; - Number ; - (make-addition MathProblem MathProblem) ; - (make-subtraction MathProblem MathProblem) ; - (make-multiplication MathProblem MathProblem) ; - (make-division MathProblem MathProblem) ; ; INTERPRETATION: A MathProblem is either simply a Number, or ; the addition of two sub-problems, or the subtraction of ; two sub-problems, ... Design the function calculate, which accepts a MathProblem and returns a Number, representing the result of the problem. For example
(check-expect (calculate (make-multiplication (make-subtraction 4 5) 99)) -99)
Challenge: Generalize the definition above to a single struct definition, (make-binop ..........). How can you extract the similarities between the four operator structures above? Can you define new operators as a result? And as a final benefit, how can you simplify calculate as a result?
Exercise 2 Recall the following data definition of Tweets (we’ll ignore image-tweets for this problem):
(define-struct tweet [id author text]) (define-struct retweet [id author tweet]) ; A Tweet is one of: ; - (make-tweet PositiveInteger String String) ; - (make-retweet PositiveInteger String Tweet) ; INTERPRETATION: A tweet is either a message (a make-tweet), ; or a retweet of another tweet (a make-retweet). All tweets have unique ; identifiers and authors. ; A Feed is a [List-of Tweet] ; INTERPREATION: A list of the Tweets in a user's feed. Design the function delete-tweet that accepts a Feed and a PositiveInteger and returns a Feed that removes any tweet with that identifier. Retweets of tweets with that identifier should also be removed. For example
(check-expect (delete-tweet (list (make-tweet 17 "Alan" "Hi!") (make-tweet 39 "Ben" "It's raining"”) (make-retweet 44 "Ben" (make-tweet 17 "Alan" "hi"))) 17) (list (make-tweet 39 "Ben" "It's raining")))
Graded Exercises
For this assignment you will simply be writing helper functions for use in the overall Forum assignment. You should copy your data definitions from Assignment 8b into an empty new file for this assignment. You will submit these data definitions and the functions below (and any test for them) for this assignment. For the next assignment, copy these functions back into your overall Forum project.
Wherever possible, use existing list abstractions to help simplify your code.
Stage 3 – New helper functions:
Design a function replace-post : History Post -> History, that searches for a Post in the given History that has the same ID# as the given Post, and replaces it with the given Post.
Hint: You may implement this function either using map or using the recursive list template. As stated in Assignment 8b, you may assume that Post’s ID#s are unique.
Hint: You will likely want to have a small helper function, to check if two Posts have the same ID#s.
Design a function add-reply : History ServerMsg -> History that takes a current History and a "REPLY" ServerMsg, and adds a Reply to the relevant Post in the History – you need to find the Post in the History with the same ID# as the one present in the reply message, create a new post that adds the reply to the previous list of replies, and then uses replace-post to create a new History with the updated post.
This add-reply function will be used in the next assignment.
Hint: if you’ve followed the data definitions from last week, the assoc function will be very helpful here. You can think of it as having the signatureYou will not likely need to use any list abstractions here, but you may well want to use local.Design a function draw-post-and-replies : Post -> Image, that takes a Post and renders both it and any of its Replys. You should distinguish the replies somehow (either by font, or indentation, or something) so that it is obvious which is the post and which are the replies. When you render an individual Post or Reply, you must show the author’s name as well as the content.
Hint: You’ve already designed a function very, very similar to this, in Lab 6 Creating Abstractions.