1

Possible Duplicate:
Flatten a list using only the forms in “The Little Schemer”

I simply want to turn '(a b (c d) e) into '(a b c d e) (i.e., insert each member of the inner list into the outer list, while maintaining the order of the elements).

There has to be a simple way to do this, but it eludes me for some reason.

Community
  • 1
  • 1
  • Spoiler: http://stackoverflow.com/a/7324493/13. (If this is for homework, please don't read that thread until you've tried solving it first!) – C. K. Young Sep 28 '12 at 00:50

1 Answers1

0

You see that '(a b (c d) e) is a tree, don't you? Something like

a---b
|\--c--d
 \--e 

In this representation, nodes are lists, and leafs are symbols. What you could do, it print all the leafs of the tree:

  1. if the tree is empty (= is the empty list), then stop
  2. If the tree is not empty, then
    1. If the tree's CAR is a symbol, print it, then process the CDR of the tree
    2. If the tree's CAR is a list, process the CAR of the tree, then process the CDR o the tree

Once you do that, you'll replace "print" by "put the value somewhere". Hopefully, that should lead you to what you want.

Axioplase
  • 183
  • 11