Take the 2-minute tour ×

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.

share|improve this question

marked as duplicate by Barmar, iMat, jonsca, Vikdor, Jason Sturges Sep 28 '12 at 3:16


Spoiler: stackoverflow.com/a/7324493/13. (If this is for homework, please don't read that thread until you've tried solving it first!) –  Chris Jester-Young Sep 28 '12 at 0:50

1 Answer 1

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


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.

share|improve this answer