[plt-scheme] How to abstract in this situation

From: wooks (wookiz at hotmail.com)
Date: Thu Jan 25 03:35:53 EST 2007

This follows on from the word permutations problem I posted recently.

Case 1
Generate all word permutations from an alphabet of length n where the
alphabet can contain letters of length i and i can be > 1.
Case 2
A variation to generate all permutations of words of length n or less.

An interesting case is if your alphabet is {"ab" "ba"} then you will
never be able to generate a word of length n where n is odd.

I ended up with 2 variations of the programs that differ in how they
handle the terminating condition.

Case 1
(let this and lambda that etc

  [(= x y) currentcomputedvalue]
  [else (recurse currentcomputedvalue)

Case 2
(let this and  lambda that etc

  [(> x y) startingvalue]
  [(= x y) currentcomputedvalue]
  [else append (filter startingvalue) (recurse currentcomputedvalue)])

The "let this and lambda that" bits are identical in both programs, as
are the first cond line in Case 1 and the 2nd cond line in Case 2.

In seeking to combine them is there a way  I can do better than the
obvious pass a parameter denoting which case I am running and  then

If Case1 
   code for Case1
   code for Case2

Posted on the users mailing list.