Lisp反向“全部”功能 [英] Lisp Reverse "all" Function

查看:126
本文介绍了Lisp反向“全部”功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在lisp中编写一个函数,使用map函数从列表中反转所有元素,但我不知道如何启动这个..我想我必须使用内置的反向函数。
例如,如果我有列表(1 2 3(4 5 6(7 8 9)))我会得到(((9 8 7)6 5 4)3 2 1)
或如果我有(1 2 3(4 5)(6 7))我会得到((7 6)(5 4)3 2 1)..
任何帮助是赞赏!

解决方案

只是一个快速的答案,不确定效率/优点:

 (defun reverse-deep(list)
(mapcar#'(lambda(li)
(cond
((consp li)(reverse-deep li))
(t li)))
(反向列表)))


I want to write a function in lisp that reverses all elements from the list using map functions but I don't have any idea how to start this.. I think I have to use the built in reverse function somehow.. For example if I have the list (1 2 3 (4 5 6 (7 8 9))) I would get (((9 8 7) 6 5 4) 3 2 1) or if I had the list(1 2 3 (4 5) (6 7)) I would get ((7 6) (5 4) 3 2 1) .. Any help is appreciated!

解决方案

Just a quick answer, not sure about efficiency/elegancy:

(defun reverse-deeply (list)
   (mapcar #'(lambda (li) 
               (cond
                ((consp li) (reverse-deeply li))
                (t li)))
           (reverse list)))

这篇关于Lisp反向“全部”功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆