views:

123

answers:

2

Hey guys,
sorry to overflow with so many questions.

I have the following:

(defun recursive-function (string) "returns list of strings"
;I am trying to return flat list
; create list L
(append (mapcar 'recursive-function L)))

But since recursive-function returns a list, I end up with a list of a list of a list..., whereas I want just a flat list.

What is the proper way for me to implement recursion on functions which take a scalar and return a list of scalars?

Thanks.

+2  A: 

If I understood correctly, you can combine reduce and append to flatten the list before returning it.

Example:

(reduce 'append '((1) (2) (3)))

Output:

(1 2 3)

In your case this might work:

(reduce 'append (mapcar 'recursive-function L))
Nick D
+1  A: 

I belive you are looking for mapcan:

[...] mapcan and mapcon are like mapcar and maplist respectively, except that the results of applying function are combined into a list by the use of nconc rather than list. [...]

(defun recursive-function (string) "returns list of strings"
   ;I am trying to return flat list
   ; create list L
   (mapcan 'recursive-function L))
Johan Kullbom