deep-reverse SCIP-2.27

FreemanRed7 posted @ Tue, 19 Mar 2013 05:55:18 -1100 in lisp学习 with tags list deep-reverse , 1093 readers

题目:以一个表为参数,返回另一个表作为值。结果表中的元素反转过来,其中的子树也反转。

例如:

输入:(list (list (list (list 2 3 99) (list 3 22 44 4) (list 6)) (list 0 2 3 7 8)) (list 9 10 11))

输出:(list (list 11 10 9) (list (list 8 7 3 2 0) (list (list 6) (list 4 44 22 3) (list 99 3 2))))

 

图形显示更直观,我手头没有好的工具,本来想画在稿纸上拍了传上来,但感觉太麻烦了。。。

 

我首先想到解决方法1如下:

(define (deep-reverse item)
  (cond ((not (pair? item)) item)
        ((null? (cdr item)) (deep-reverse (car item)))
        (else (list (deep-reverse (cdr item))
                    (deep-reverse (car item))))))

代码原意:

代码第二行用于递归到叶子节点时返回具体的元素值(num or nil)

因为list以nil结尾,第三行用于递归到nil前叶子节点后,去掉nil

第四、五行则是进行递归调用,并将左右节点反转。

对于输入1:(list (list 2 3) (list 4 5))

    输出:(list (list 5 4) (list 3 2))

对于输入2:(list (list (list 2)))

    输出:2

对于输入3:(list (list 2 3 4 5 ) (list 6 7 8))

    输出:(list (list (list 8 7) 6) (list (list (list 5 4) 3) 2))

 

错误分析:

对于输入2,(cdr item)一直为nul,不断求(car item)最后返回2

对于输入3,当list中元素超过2个时,(list (deep-reverse (cdr item)) (deep-reverse (car item)))会将最后两个元素组合为list,并不断与前面的每个元素组合为list。

总结:方法1属于针对(list (list 2 3) (list 4 5))这种情况进行的特殊处理,结果遇到其它list类型就完全失效了。

 

我苦思冥想了好几天,也没找到好的方法--脑袋确实比较笨。。。

想不通的地方是:对于list A: (list 2 3 4)与list B: (list (list 2 3) (list 4)),它们都会递归得到(list 4)。

                但list A中(list 4)应该与前面的叶子组合为一个list,而list B中(list 4)则应该与前面的list组合为一个复合list。

                如何区分这两中不同的形式?

                原来用list连接(deep-reverse (cdr item))与(deep-reverse (car item)),这样的做法也不可取,那应该采取什么形式呢?

 

我在纸上笔画了好多天,完成了一个比较丑陋的解法(单纯觉得代码的形式不舒服),方法2如下:

(define nil '())

;将list a与b连接起来,b可以是list也可以不是
(define (append a b)
  (if (null? a)
      (if (pair? b)
          (list b)
          (cons b nil))
      (cons (car a) (append (cdr a) b))))          

;处理反转过程中的nil结束符,nil的出现也意味着list的创建                           
(define (list-nil x y)
  (if (null? x)
      (list y)
      (append x y))) 

(define (deep-reverse item)
  (if (not (pair? item)) 
      item
      (list-nil (deep-reverse (cdr item))
                (deep-reverse (car item)))))

 

 

PS:在思考这个问题的过程中还想到一个比较诡异的方法,但下面的方法3没有实现:

;直接将所有元素反转,然后再去掉前面的nil
(define (deep-reverse item)
  (cond ((not (pair? item)) item)
        (else (list (deep-reverse (cdr item))
                    (deep-reverse (car item))))))

;(define (del-front-nil item)
;  (if (= nil (car item))
;      (del-front-nil (cdr item))))

思路是:先不管三七二十一,将所有的叶子反转,包括结束符nil,然后再构造一个函数去掉每个子树最前面的nil,但是去掉nil的函数非常难写,我还没完成。。。所以在这里仅提出这样一个思路吧。

 

不知道这道题有其它解法不,看起来更漂亮。

SICP的习题答案可以参考如下:

http://eli.thegreenplace.net/category/programming/lisp/sicp/

里面这道题的解法写得也不是很好看。。。

Avatar_small
seo service london said:
Thu, 22 Feb 2024 00:02:22 -1100

I just thought it may be an idea to post incase anyone else was having problems researching but I am a little unsure if I am allowed to put names and addresses on here

Avatar_small
먹튀검증 said:
Tue, 27 Feb 2024 19:50:18 -1100

Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work.

Avatar_small
카지노탐구생활 said:
Tue, 27 Feb 2024 19:50:39 -1100

Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work.

Avatar_small
먹튀폴리스 said:
Tue, 27 Feb 2024 19:51:01 -1100

I’ve been surfing on-line greater than three hours as of late, but I by no means discovered any fascinating article like yours. It’s pretty price enough for me. Personally, if all webmasters and bloggers made excellent content material as you did, the internet can be a lot more helpful than ever before. “Perfection of moral virtue does not wholly take away the passions, but regulates them.” by Saint Thomas Aquinas.

Avatar_small
토토사이트추천 said:
Tue, 27 Feb 2024 19:51:25 -1100

I know this is one of the most meaningful information for me. And I'm animated reading your article. But should remark on some general things, the website style is perfect; the articles are great. Thanks for the ton of tangible and attainable help.

Avatar_small
토토사이트순위 said:
Tue, 27 Feb 2024 19:51:42 -1100

Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I've been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates

Avatar_small
토담토담 said:
Tue, 27 Feb 2024 21:27:30 -1100

Hello, i study your blog often and i own an analogous one and i used to be just wondering if you get much of spam comments? If so how do you catch it, any plugin or anything you’ll be able to advise? I get thus often lately it’s driving me crazy so any help is very abundant appreciated. for older folks, retirement or a huge promotion at work is a time when celebration events are going to be a great idea.

Avatar_small
먹튀검증커뮤니티 said:
Tue, 27 Feb 2024 21:30:55 -1100

I was intrigued accepting that you at whatever point imagined of changing the plan of your site? Its very much made I genuinely like what you've got to say. However, no doubt you might somewhat more in the technique for material at some point so individuals could get along with it better.

Avatar_small
토토사이트 said:
Tue, 27 Feb 2024 21:44:30 -1100

Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work.

Avatar_small
ok토토먹튀검증 said:
Tue, 27 Feb 2024 21:53:14 -1100

Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I've been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates

Avatar_small
슬롯나라 said:
Tue, 27 Feb 2024 21:54:22 -1100

Fantastic blog! Do you have any tips and hints for aspiring writers? I’m planning to start my own website soon but I’m a little lost on everything. Would you suggest starting with a free platform like Wordpress or go for a paid option? There are so many options out there that I’m completely overwhelmed .. Any tips.

Avatar_small
먹튀검증 said:
Tue, 27 Feb 2024 21:57:02 -1100

I'm very drained on get together with to find key and sharp talk concerning this. I will in all probability be returning the future to your weblog for even more soon. It seems like you consume a ton of energy. I'm genuinely getting a charge out of looking at your meticulously made articles

Avatar_small
메이저사이트 said:
Tue, 27 Feb 2024 21:57:18 -1100

Fantastic blog! Do you have any tips and hints for aspiring writers? I’m planning to start my own website soon but I’m a little lost on everything. Would you suggest starting with a free platform like Wordpress or go for a paid option? There are so many options out there that I’m completely overwhelmed .. Any tips.

Avatar_small
스포츠토토 said:
Tue, 27 Feb 2024 21:57:33 -1100

Once I originally commented I clicked the -Notify me when new comments are added- checkbox and now each time a remark is added I get four emails with the identical comment. Is there any way you could possibly un sub me from the service? Thanks!

Avatar_small
안전토토사이트 said:
Tue, 27 Feb 2024 21:57:52 -1100

Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I've been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates

Avatar_small
안전놀이터 said:
Tue, 27 Feb 2024 21:58:10 -1100

Hello, i study your blog often and i own an analogous one and i used to be just wondering if you get much of spam comments? If so how do you catch it, any plugin or anything you’ll be able to advise? I get thus often lately it’s driving me crazy so any help is very abundant appreciated. for older folks, retirement or a huge promotion at work is a time when celebration events are going to be a great idea.

Avatar_small
메이저사이트 said:
Tue, 27 Feb 2024 21:59:17 -1100

Fantastic blog! Do you have any tips and hints for aspiring writers? I’m planning to start my own website soon but I’m a little lost on everything. Would you suggest starting with a free platform like Wordpress or go for a paid option? There are so many options out there that I’m completely overwhelmed .. Any tips.

Avatar_small
카지노헌터 said:
Tue, 27 Feb 2024 23:53:08 -1100

i read a lot of stuff and i found that the way of writing to clearifing that exactly want to say was very good so i am impressed and ilike to come again in future..

Avatar_small
카지노사이트목록 said:
Tue, 27 Feb 2024 23:53:27 -1100

I'm very drained on get together with to find key and sharp talk concerning this. I will in all probability be returning the future to your weblog for even more soon. It seems like you consume a ton of energy. I'm genuinely getting a charge out of looking at your meticulously made articles

Avatar_small
토토사이트추천 said:
Tue, 27 Feb 2024 23:53:48 -1100

Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I've been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates

Avatar_small
เว็บไซต์ แทงบอลออนไล said:
Tue, 27 Feb 2024 23:54:06 -1100

Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work.

Avatar_small
사설토토사이트 said:
Wed, 28 Feb 2024 00:28:05 -1100

A decent blog dependably concocts new and energizing data and keeping in mind that understanding I have feel that this blog is truly have each one of those quality that qualify a blog to be a one.

Avatar_small
먹튀검증커뮤니티 said:
Wed, 28 Feb 2024 00:28:37 -1100

I know this is one of the most meaningful information for me. And I'm animated reading your article. But should remark on some general things, the website style is perfect; the articles are great. Thanks for the ton of tangible and attainable help.

Avatar_small
토토사이트홍보 said:
Wed, 28 Feb 2024 00:29:02 -1100

Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work.

Avatar_small
퍼스트카지노 said:
Wed, 28 Feb 2024 02:13:46 -1100

I am writing to let you understand what a extraordinary discovery my cousin’s daughter had going through your web page. She realized a lot of details, including how it is like to have a wonderful helping style to have certain people without problems gain knowledge of a number of grueling matters. You really surpassed her expected results. Many thanks for giving the helpful, healthy, revealing as well as cool tips about that topic to Evelyn.

Avatar_small
토토사이트종류 said:
Wed, 28 Feb 2024 02:14:08 -1100

Hello, i study your blog often and i own an analogous one and i used to be just wondering if you get much of spam comments? If so how do you catch it, any plugin or anything you’ll be able to advise? I get thus often lately it’s driving me crazy so any help is very abundant appreciated. for older folks, retirement or a huge promotion at work is a time when celebration events are going to be a great idea.

Avatar_small
먹튀검증사이트 said:
Wed, 28 Feb 2024 02:14:39 -1100

Fantastic blog! Do you have any tips and hints for aspiring writers? I’m planning to start my own website soon but I’m a little lost on everything. Would you suggest starting with a free platform like Wordpress or go for a paid option? There are so many options out there that I’m completely overwhelmed .. Any tips.

Avatar_small
먹튀365 said:
Wed, 28 Feb 2024 02:52:51 -1100

Once I originally commented I clicked the -Notify me when new comments are added- checkbox and now each time a remark is added I get four emails with the identical comment. Is there any way you could possibly un sub me from the service? Thanks!

Avatar_small
안전토토사이트 said:
Wed, 28 Feb 2024 02:53:19 -1100

Hello, i study your blog often and i own an analogous one and i used to be just wondering if you get much of spam comments? If so how do you catch it, any plugin or anything you’ll be able to advise? I get thus often lately it’s driving me crazy so any help is very abundant appreciated. for older folks, retirement or a huge promotion at work is a time when celebration events are going to be a great idea.

Avatar_small
메이저안전놀이터 said:
Wed, 28 Feb 2024 02:53:45 -1100

Fantastic blog! Do you have any tips and hints for aspiring writers? I’m planning to start my own website soon but I’m a little lost on everything. Would you suggest starting with a free platform like Wordpress or go for a paid option? There are so many options out there that I’m completely overwhelmed .. Any tips.

Avatar_small
토토팡 said:
Wed, 28 Feb 2024 03:03:06 -1100

I know this is one of the most meaningful information for me. And I'm animated reading your article. But should remark on some general things, the website style is perfect; the articles are great. Thanks for the ton of tangible and attainable help.

Avatar_small
바카라사이트 said:
Wed, 28 Feb 2024 03:03:22 -1100

I was intrigued accepting that you at whatever point imagined of changing the plan of your site? Its very much made I genuinely like what you've got to say. However, no doubt you might somewhat more in the technique for material at some point so individuals could get along with it better.

Avatar_small
먹튀검증사이트 said:
Wed, 28 Feb 2024 03:03:39 -1100

Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I've been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates

Avatar_small
사설토토 said:
Wed, 28 Feb 2024 03:04:16 -1100

Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I've been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates

Avatar_small
메이저사이트목록 said:
Wed, 28 Feb 2024 03:04:40 -1100

Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work.

Avatar_small
먹튀검증사이트 said:
Wed, 28 Feb 2024 03:05:45 -1100

Nice post. I was continuously checking this blog and I am impressed! Extremely useful info particularly the last part .I care for such information much. I was seeking this particular info for a long time. Thank you and good luck .

Avatar_small
먹튀사이트 said:
Wed, 28 Feb 2024 03:06:08 -1100

I'm very drained on get together with to find key and sharp talk concerning this. I will in all probability be returning the future to your weblog for even more soon. It seems like you consume a ton of energy. I'm genuinely getting a charge out of looking at your meticulously made articles


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter