...
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.
HTML |
---|
<ol> |
HTML |
---|
<li> |
- Develop a program
count-positive
which takes a list of numbers and returns a count of how many are positive, i.e., greater than zero.
Use the stepper oncount-positive
applied to a list of length 2. How many times is your function called (counting both the initial call and recursive calls)?
...
HTML |
---|
</li> |
...
...
<li>
- Develop a program
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.,
Later in the course, we'll generalize this idea of mapping some operation over a list to generate a new list of the results.Code Block > (map-add 3 (cons 1 (cons 4 (cons 8 empty)))) (cons 4 (cons 7 (cons 11 empty)))
...
HTML |
---|
</li> |
...
...
<li>
- Develop a program
filter-positive
which takes a list of numbers and returns a list of all those numbers which are positive. E.g.,
Later in the course, we'll generalize this idea of filtering a list by some test condition.Code Block > (filter-positive (cons 1 (cons -5 (cons 3 empty)))) (cons 1 (cons 3 empty))
...
HTML |
---|
</li> |
...
Functions on Structures
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
...