Index: Example: project, Example: permutations
Here is a solution to the second exam's first problem. This version is longer than necessary, to simplify the next step.
To do: Develop a version using filter and map.
To do: Replace the use of the named helper function okay? with an anonymous function using lambda.
The versions you have been working with all have the property that they make two distinct passes over lists. Compare them to a version that is like what most of you provided on the exam, which only makes one pass over a list. Often when editing to use abstract functions, you must split a single recursive function into several pieces, where each piece corresponds to using an abstract function.
As extra credit on a homework, you may have previously written a program to compute all the permutations (or "arrangements") of a list. Here is a solution that follows the book's hints.
To do: Look at and use the provided solution, familiarizing yourself with how the code works.
To do: Make test cases for each of the helper functions. We have provided test cases for the overall function.
To do: Write a version of permutations using map and foldr. Hints:
To do: Write a version of the the testing code set-equal? using map and foldr. Hint: Our solution "cheats" and uses andmap and ormap which can be defined as the following:
(define (andmap f l) (foldr (lambda (f r) (and f r)) #t (map f l))) (define (ormap f l) (foldr (lambda (f r) (or f r)) #f (map f l)))