Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用map实现Lisp列表线性化_Lisp_Map Function - Fatal编程技术网

用map实现Lisp列表线性化

用map实现Lisp列表线性化,lisp,map-function,Lisp,Map Function,是否可以使用映射函数将列表线性化? 例如:(12(3(45)6))->(123456) 我的错误做法: (defun f1 (x) (cond ((atom x) x) (t (f2 x)) ) ) (defun f2 (lst) (mapcar 'f1 lst) ) 我不会称之为线性化列表,而是扁平化列表 (defun flatten (lst) "Flatten the list lst" (cond ((null lst)

是否可以使用映射函数将列表线性化?
例如:(12(3(45)6))->(123456)

我的错误做法:

(defun f1 (x)
    (cond
        ((atom x) x)
        (t (f2 x))
    )
)

(defun f2 (lst)
    (mapcar 'f1 lst)
)

我不会称之为
线性化列表
,而是扁平化列表

(defun flatten (lst)
"Flatten the list lst"
  (cond ((null lst) nil)
        ((atom lst) (list lst))
        (t (loop for e in lst appending (flatten e)))))

是的,地图,但不是
mapcar
,而是
mapcan

(defun flatten (lst)
  (mapcan
     #'(lambda (a)
         (cond
           ((atom a) (list a))
           (T (flatten a))))
     lst))
如果你说的Lisp是Scheme或类似的东西,试着用
append-map
flat-map
flatMap
mapcat
来代替常见的Lisp的
mapcan

Wow?直到。