![]() |
|
See the Exam Resources
Example of the same function written with both forward and reverse accumulation: sumbig.scm (New and improved! With alternate implementations now!)
Example of the speed increase gained by using forward accumulation: speed_test.scm
Example of processing a list of abstract data structures: pizzalist.scm
Example of multiple accumulators: sumlast2.scm
Example of using both forward and reverse accumulation in the same function:
Consider the problem of detecting if a list contains any duplicate elements in a list. In pseudo-code we see:
- Base case: false -- there are no duplicates in an empty list
- Inductive case: Return true if either
- first is to be found in the rest of the list --> need helper, forward accumulation process
OR- there are duplicates in the rest of the list --> straight recursive call --> reverse accumulation process
The key is to write exactly what you think is supposed to be happening: duplicates.scm
©2003 Stephen Wong