2.17 Introduction to Mutation
When you go from right
to left, that is induction
Left to right: Accumulator
2.17.1 Objectives
what accumulator style is
write functions in accumulator style
2.17.2 Exercises (due 2011/11/18 08:45:00)
Note: When you write you functions in accumulator style, you must decide on an accumulator argument. I have not given that to you below, therefore when you add it, it is not considered violating the contract, unless your main function (the one you use for test cases) takes the argument as well.
1. Write the function product, which takes a list of numbers and returns their product, in accumulator style.
2. Write the function palindrome, which takes a list of numbers and returns a palindrome of that list. For example, on the list 1:2:3:! it should return 1:2:3:2:1:!.
3. Write the function to10, which takes a list of digits and returns the corresponding number, where the first number is the most significant digit. Thus, 1:2:3:! should return 123. (This is unlike the convert functions we’ve seen before.)
4. Write the function isPrime, which takes an integer and returns true if the number is prime. Recall that a number is prime if it is not divisible by any number except itself and 1. Recall that a % b is the remainder after dividing a by b. (Hint: This uses the natural number data definition.)
2.17.3 Optional Exercises (due 2011/11/22 08:45:00)
5. Generalize to10 to toB which takes an additional "base" argument that controls the numeric base of the digit sequence.
6. Write the function howMany, which takes a list of strings and returns how many letters are in all the strings combined, in accumulator style.
2.17.4 Notes
These notes are primarily for my sake, but I don’t see any reason to hide them from you.
write factorial functionally (show the natural number data definition using 0 and n+1) |
|
write factorial in accumulator style (saves space on stack---don't say the stack, instead talk about how when we do substitution the program gets really big) |
- point out that the order of addition is different |
- we need to know what the initial argument is (it moves from the empty code to the caller---this breaks the abstraction) |
|
write reverse (from #13) in accumulator style (save space and time, because we can write "putAtEnd" better i.e. we just cons onto the front going down in the acc) |
|
writing a purpose for an accum function is difficult, because the answer depends on the accumulator argument |
|
make a data definition for a phone book (list of phone entry (name and number)) |
write the following FUNCTIONS: |
write lookup : name phone-book -> number [must be backed up by method] |
write add : name number phone-book -> phone-book |
|
End with a weird thing: |
|
int i = 0; |
printf("The answer is %d, but should be %d\n", i, 0); |
printf("The answer is %d, but should be %d\n", i, 0); |
printf("The answer is %d, but should be %d\n", i, 0); |
printf("The answer is %d, but should be %d\n", i, 0); |
printf("The answer is %d, but should be %d\n", i, 0); |
i = 1; |
printf("The answer is %d, but should be %d\n", i, 0); |
|
WHAT???? |