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

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

问题描述

我想用lisp编写一个函数,该函数使用map函数反转列表中的所有元素,但是我不知道如何启动它。.我想我必须以某种方式使用内置的反转函数。 b $ b例如,如果我有列表(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)..
任何帮助都将得到赞赏!

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 Reverse&“全部”功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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