![]() |
![]() |
|
Due
This homework is shorter than others, and doubles as an exam-prep.
Note: I may adjust the last problem(s) of this homework by this evening, Oct.08 (Tue). In particular, I may include one question involving (ancestor-)trees, as we've discussed in lecture, after reviewing the resulting length and deadline.
Your name and the assignment-number must be in a comment at the start of the file, and your hardcopy must be stapled. All functions/data must include the appropriate steps1 of the-design-recipe.html. In particular, test cases alone might be worth half the credit for a function. Unless otherwise indicated, two test cases will suffice for most problems, if they are testing different situations.
For this (and future) assignments, bump up your DrRacket language-level to Intermediate Student with lambda. Do not call any of the following functions:
Recall: the scope of identifiers introduced with let is just the body of the let, while the scope of identifiers introduced with let* is the body of the let and all following right-hand-sides of the let*.
DrRacket's Check-Syntax: Yes, you can answer this question by putting the code in DrRacket and clicking the Check Syntax button, and then mousing over a to see the arrows you'll write, as well as verify the result. But first attempt it “by hand”, since if it's on the exam you won't have DrRacket with you.In all cases, presume we have:
line 01 (define a 5) line 02 (define b 10) |
line A1 (let {[z 50] line A2 [a 51] line A3 } line A4 (+ a b z)) ; evaluates to: , with one use of a: → |
line B1 (let {[z 50] line B2 [a 51] line B3 [b (* a 3)] line B4 } line B5 (+ a b z)) ; evaluates to: , with two uses of a: → and → . |
line C1 (define (foo a) line C2 (let {[z 50] line C3 [a 51] line C4 [b (* a 3)] line C5 } line C6 (+ a b z))) line C7 (foo 1001) ; evaluates to: , with two uses of a: → and → . |
line D1 (let* {[z 50] ;note: let*, not let. line D2 [a 51] line D3 [b (* a 3)] line D4 } line D5 (+ a b z)) ; evaluates to: , with two uses of a: → and → . |
Who doesn't love binge-watching TV shows?2 After extensive research to determine what aspects of a TV series are important for binge-watching information, I have personally determined that most shows have plot threads which may span several episodes. It's not even important what the plot threads are; we can abstract away to just counting how many plot threads there are.
A binge-task is one of:
Be nice., or
A fool might show great wisdom, like Phoebe. (Or might not, like Joey.)); and the remaining-show — a binge-task.
List-like, but not lists: Although binge-tasks are not lists, they are certainly reminiscent of them: For example, finales and shark-jumps (like empty-lists) cannot possibly followed by any additional tasks. However, cliff-hangers and morals (like cons structs) do have information about an episode, “followed” by one more binge-task (which may be another cliff-hanger which itself contains yet another binge-task, etc.).Compare this to how we represent a list of a thousand numbers really being a cons struct with only two fields: the first number, and the remainder of the list (which is another cons which itself contains yet another cons, etc.).
So: Do not use built-in lists; everything you need comes straight from the definition above.
Hint: In cases where you don't have more than one piece of information to track, you don't need to introduce any compound-data.
warning: Make sure that you can distinguish between a shark-jump, and a finale that happens to resolve two threads (they are different). Also, do not try to represent all the above using a single struct -- one which contains a number-of-threads-introduced and a number-resolved and some sort of tag, plus further fields that are only meaningful depending on the tag. That would be sloppy data-modeling; a shark-jump struct/object shouldn't contain fields that are meaningless, nor fields whose value is a function of other fields3.
(define finale0 (define shark0 (define moral1 (define cliffhanger1 (define |
(check-expect (count-morals finale0) ) (check-expect (count-morals shark0) ) (check-expect (count-morals moral1) ) (check-expect (count-morals cliffhanger1) ) (check-expect (count-morals ) ) |
Note: If somebody starts a binge-taskhalfway through, this might be a negative number, if they see a thread resolved that they never saw originally introduced4 .
(check-expect (net-unresolved finale0) ) (check-expect (net-unresolved shark0) ) (check-expect (net-unresolved moral1) ) (check-expect (net-unresolved cliffhanger1) ) (check-expect (net-unresolved ) ) |
(check-expect (bullseye 3) (underlay (circle 30 'solid 'blue) (circle 20 'solid 'red) (circle 10 'solid 'blue))) |
data def: Recall: A natural-number is:
The built-in predicates zero? and positive? are often used to distinguish the two branches, though of course = and > could be used equally-well.
- 0, OR
- (+ 1 [natural-number])
And/or, see the lecture's notes on natnums as recursive data[code: before, after], about viewing sub1 as the getterto pull out thenatural-number field, which a positive is built out of.
processing the natnum datatype definition:
Write my-take, which takes a list and a number,
and returns the list containing the that many items from the front of the list.
Big picture: One point of all these exercises (and, last week's list-processing) is to note that they are all examples of the exact same problem-solving recipe — a union-of-structs, where some struct-fields are themselves the overall union-type. (the composite pattern). So all these are not really different topics at all! Even if the exam doesn't specifically have any list-processing questions, it can still check for understanding of the underlying structure.
This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |