Originally created by Dr. Griener
Instructions for students & labbies: Start with short group discussion. Students use DrScheme on the exercises at their own pace, while labbies wander among the students, answering questions, bringing the more important ones to the lab's attention. Example solutions are attached at the bottom of the page.
All of these examples use the same input type: a list of numbers. So, don't forget to reuse what you can to save yourself some time. Use your data examples (i.e., sample lists of numbers) for each of the function examples (i.e., what the function does on each of those inputs). Copy-and-paste the type's template as your starting point for each function.
count-positive
which takes a list of numbers and returns a count of how many are positive, i.e., greater than zero.count-positive
applied to a list of length 2. How many times is your function called (counting both the initial call and recursive calls)?map-add
which takes a number and a list of numbers and returns a list of numbers, where the resulting list is constructed by adding the given number to each element in the list. E.g.,
> (map-add 3 (cons 1 (cons 4 (cons 8 empty)))) (cons 4 (cons 7 (cons 11 empty))) |
filter-positive
which takes a list of numbers and returns a list of all those numbers which are positive. E.g.,
> (filter-positive (cons 1 (cons -5 (cons 3 empty)))) (cons 1 (cons 3 empty)) |
We've seen lists of symbols and lists of numbers; you can of course have a list with elements of any given type--including other structures like _posn_s. This exercise just uses ideas you've already learned, but in new contexts.
positive-quadrant?
, which takes in a single posn
and returns true only when its components are both positive.count-positive-quadrant
, which takes a list of _posn_s and returns a count of those in the upper-right quadrant.filter-positive-quadrant
, which takes a list of _posn_s and returns a list of those in the upper-right quadrant.Here are two reasonable definitions for non-empty lists.