scheme 自写简单的读取列表函数
lisp吧
全部回复
仅看楼主
level 4
gionie 楼主
(define (findme count thelist) ;findme接受2个参数,要寻找的位置和一个列表
(if (= count 1)
(car thelist)
(findme (- count 1) (cdr thelist))))
(define x (list 11 22 33 44 55 (list 66 77) 88 99))
> (findme 6 x)
'(66 77)
> (findme 1 (findme 6 x))
66
findme函数并没有对传入的thelist进行判断是否为列表。仅供学习参考。
刚开始写findme函数的时候,总想一步到位。在写尾递归时,百思不得如何处理car部分。后来想想退而求其次,递归出(cdr thelist)也不错。
下面是cdr版
(define (findme count thelist)
(if (= count 1)
thelist
(findme (- count 1) (cdr thelist))))
当调试完后恍然大悟,因为输出的是thelist,而thelist经过递归后已经是cdr部分。那么直接在输出的时候将thelist 进行 (car thelist)就取得了我们需要的部分。
2015年12月11日 02点12分 1
level 4
gionie 楼主
消灭0回复....................................
2015年12月11日 02点12分 2
level 11
nice
2015年12月13日 19点12分 3
level 5
不明觉厉
2015年12月13日 22点12分 4
1