This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: CS 135 Winter 2012 Brad Lushman Assignment: 4 Due: Wednesday, February 8, 2012 9:15am Language level: Beginning Student Allowed recursion: Structural recursion Files to submit: list.rkt , daily-balance.rkt , area.rkt Warmup exercises: HtDP 8.7.2, 9.1.1 (but use box-and-pointer diagrams), 9.1.2, 9.5.3 Practise exercises: HtDP 8.7.3, 9.5.4, 9.5.6, 9.5.7 Overall instructions for this assignment are the same as previous assignments except you will note a new addition at the top: “Allowed recursion” restricts you to structural recursion – recursion that follows the data definition of the data it consumes. Here are the assignment questions you need to submit. 1. Perform the Assignment 4 questions using the online evaluation “Stepping Problems” tool linked to the course web page and available at https://www.student.cs.uwaterloo.ca/ ˜ cs135/stepping The instructions are the same as A03; check there for more information, if necessary. Re- minder: You should not use DrRacket’s Stepper to help you with this question, for a few reasons. First, as mentioned in class, DrRacket’s evaluation rules are slightly different from the ones presented in class; you are to use the ones presented in class. Second, in an exam sit- uation, of course, you will not have the Stepper to help you. Third, you can re-enter steps as many times as necessary to get them correct, so you might as well maximize the educational benefit. 2. This problem involves lists. Of the built-in list functions, you may only use cons , first , rest , empty? , and cons? . In the file list.rkt : (a) Use the template for functions that consume a ( listof Num ) to write count-positives , which consumes a ( listof Num ) and produces a count of all of the positive numbers in the list. (b) Use the template to write the predicate some-negative? , which consumes a ( listof Num ) and produces true if and only if at least one number in the list is negative. (c) Use the template to write the function reciprocals , which consumes a ( listof Num ) and produces the list resulting from transforming each number in the list into its reciprocal. Every occurrence of 0 in the original list should be transformed to ’ error . For example, ( reciprocals ( cons 1 ( cons ( cons 2 empty )))) should produce ( cons 1 ( cons ’ error ( cons 0.5 empty ))). CS 135 — Winter 2012 Assignment 4 1 (d) For this subquestion, you should use the template for functions that process a non- empty list. Write the Scheme function differences , which consumes a non-empty list of Num , and produces the list of differences between consecutive entries. For exam- ple, ( differences ( cons- 1 ( cons 4 ( cons 5 empty )))) should produce ( cons 5 ( cons 1 empty )). Note that the list produced will always be one element shorter than the list))....
View Full Document
- Winter '07
- Recursion, Negative and non-negative numbers