方案中的递归 [英] Recursion in scheme

查看:43
本文介绍了方案中的递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是从这个列表中找到

My goal is to find from this list

(A B C D E G)

新列表

(B D G)

但是我的代码不起作用.

But my code is not working.

(define  (fun lst)
    (cond
     ((null? lst) '())
      ((null? (cdr lst) '())
       (else  (cons ( cadr lst) ( fun lst))))

我收到(B C D E G).我哪里出错了?

I'm getting (B C D E G). Where have I gone wrong?

推荐答案

您的函数没有做任何事情,而且您确实没有指定要测试的内容.如果你真的只想从那个特定的列表中得到 B D G 那么你需要做的就是对每个字符进行这些测试,(equal? (car lst) 'B) 等等.

Your function isn't doing anything and you really haven't specified what you want to test. If you really only want to get B D G from that specific list then all you need to do is make those each tests, (equal? (car lst) 'B) and so on for each character.

如果该函数假设只是打印列表中的所有其他字符,那么您需要构建一种方法来做到这一点.例如,您现在拥有的递归基本情况是正确的,空列表应该返回一个空列表.否则,如果它不为空,则返回列表的 cdr,然后使用它.

If instead the function is suppose to just print every other character in a list then you need to construct a way to do that. For example your base case for recursion that you have now is correct, and empty list should return an empty list. Otherwise if it is not empty return the the cdr of the list and then work with that.

如果你仍然想不出答案,那就开始把它写在纸上,看看不同的测试会做什么.您需要想出一种方法来找到所有其他角色.

If you still can't figure out an answer just start writting it down on paper and see what different tests will do. You need to come up with a way to find every other character.

这篇关于方案中的递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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