Scheme中具有奇数索引的列表的打印元素
所以这里有一个代码叫做ret赔率Scheme中具有奇数索引的列表的打印元素,scheme,Scheme,所以这里有一个代码叫做ret赔率 ex: (define (ret-odds lst) (if (null? lst) null (if (null? (cdr lst)) null (cons (car lst) (ret-odds (cdr (cdr lst))))))) 我知道问题在于最后一行跳过了列表的第二个元素,只给出了第三个 例如:(ret赔率(list'a'g'e))该过程跳过g和e,并给我空值,因此我只得到a,因此我想知道如何
ex: (define (ret-odds lst)
(if (null? lst) null
(if (null? (cdr lst)) null
(cons (car lst) (ret-odds (cdr (cdr lst)))))))
我知道问题在于最后一行跳过了列表的第二个元素,只给出了第三个
例如:
(ret赔率(list'a'g'e))
该过程跳过g和e,并给我空值,因此我只得到a,因此我想知道如何解决此问题?您的代码总是跳过列表中的最后一个元素:
(list'a'd'e)
进行第一次调用时,lst为非空且(cdr lst)为非空,因此它会占用汽车('a
),并使用cddr进行递归调用(即(list'e)
)(list'e)
进行第二次调用时,lst为非null,但(cdrlst)为null。因此它返回null,完全跳过'e
ex: (define (ret-odds lst)
(if (null? lst) null
(cons (car lst)
(if (null? (cdr lst)) null (ret-odds (cdr (cdr lst)))))))
将第二个
if
的null
更改为lst
(define (ret-odds lst)
(cond ((null? lst) lst)
((null? (cdr lst)) lst)
(else (cons (car lst) (ret-odds (cddr lst))))))
什么是
ret赔率
应该返回?它是这样的(ret赔率(列出'a'g'e)并返回a和e这是因为它应该得到奇数索引,所以1 3 5 an等等,不管列表有多大……a)它闻起来像是家庭作业。b) 看起来你和asdfaea是同一个人。如果是这样,试图隐藏自己的身份并不是赢得尊重的好方法。。。。