Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List Scheme:循环遍历列表并仅返回非数字的程序_List_Scheme_Racket - Fatal编程技术网

List Scheme:循环遍历列表并仅返回非数字的程序

List Scheme:循环遍历列表并仅返回非数字的程序,list,scheme,racket,List,Scheme,Racket,我试图在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 (defin

我试图在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

提前感谢您的帮助

您的第二个条件是错误的:

((number? list) '())
如果头部元素是一个数字,则不使用编号测试列表,您应该测试
(汽车列表)
,并递归处理
(cdr列表)
。请查看以下代码:

(define (up-to-first-number lst)
(cond
 ((null? lst) '())
 ((number? (car lst)) (up-to-first-number (cdr lst)))
 (else (cons (car lst) (up-to-first-number (cdr lst))))))

有了这样一个简单的程序,你应该可以手工扣除

(up-to-first-number '(1)) ; ==

(cond
  ((null? '(1)) '())
  ((number? '(1)) '())
  (else (cons (car '(1)) (up-to-first-number (cdr '(1))))))
所以
(null?'(1))
#f
正如预期的那样,但是您可能会惊讶
(number?'(1))
也是
#f
吗?也许你的虫子在某个地方


还要注意,通过调用参数
list
,标准过程
list
在该过程中不再可用。这就是为什么在Scheme代码中经常看到使用
lst
来表示列表参数的原因

就这样!汽车清单,而不仅仅是清单。这么简单的修复。非常感谢。很高兴为您提供帮助:-)如果您能接受我的回答,我将不胜感激:-)