SICP exercise 2.28

From Drewiki

Jump to: navigation, search

Problem

Write a procedure fringe that takes as argument a tree (represented as a list) and returns a list whose elements are all the leaves of the tree arranged in left-to-right order. For example,

 

Output:

(1 2 3 4)
 

Output:

(1 2 3 4 1 2 3 4)


Solution

Here's an implementation of fringe. When its input is neither nil nor a pair, it returns a list containing its input:

 

Test:

 

Output:

(1 2 3 4)
 

Output:

(1 2 3 4 1 2 3 4)
Personal tools