开发者

Lisp, a couple of questions about lists and recursion

开发者 https://www.devze.com 2022-12-19 15:55 出处:网络
sorry to overflow with so many questions. I have the following: (defun recursive-function (string) \"returns list of strings\"

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开发者_JAVA百科 '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.


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))


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))
0

精彩评论

暂无评论...
验证码 换一张
取 消