我试图在Scheme中编写一个程序,该程序需要一个列表并返回仅包含非数字项目的列表。这似乎应该工作,但它只是打印整个列表。任何人都可以看到我在这里做错了吗?Scheme:遍历列表并仅返回非数字的程序
;;;up-to-first-number
;;;takes a list as its input and returns a list containing all
;;;the elements up to the first numeric element in the input list.
;;test lists
(define mylist '(a b c 1 2 3))
(define mylist2 '(1 2 2 4 5))
(define (up-to-first-number list)
(cond
((null? list)'()) ;if list is null, return null list
((number? list) '()) ;if item is a number, return null list
(else (cons (car list) (up-to-first-number (cdr list)))))) ;else, add item to new list and recurse
在此先感谢您的帮助!
就是这样!汽车清单,而不仅仅是清单。这样一个简单的修复。谢谢!! – Ang
很高兴帮助:-)如果您能接受它作为答案,我将不胜感激:-) – shizhz