(defun rep(list)
(format t"~a~%" list)
(cond
((null list) nil)
((atom (car list)) (cons (car list) (rep (cdr list))))
((listp (car list)) (cons (car (reverse (car list))) (cdr list)))
(t (rep list))
)
)
编写一个函数,用最后一个元素替换列表的每个子列表。 子列表是第一级的元素,它是一个列表。 示例:
(a (b c) (d (e (f)))) ==> (a c (e (f))) ==> (a c (f)) ==> (a c f)
(a (b c) (d ((e) f))) ==> (a c ((e) f)) ==> (a c f)
我有以上问题要解决。得到它直到一点但我被卡住了。 显然它没有进入列表中的下一个元素,我不知道为什么。有什么想法吗?
答案 0 :(得分:0)
我会这样打破它:
(defun last-element (lst)
(if (listp lst)
(last-element (car (last lst)))
lst))
(defun rep (lst)
(when lst
(cons (last-element (car lst)) (rep (cdr lst)))))
然后
(rep '(a (b c) (d (e (f)))))
=> '(A C F)
答案 1 :(得分:0)
没有使用地图功能
(defun rep(list)
(cond
((null list) nil)
((listp (car list)) (rep (cons (car (reverse (car list))) (rep (cdr list)))))
(t (cons (car list) (rep (cdr list))))
)
)