Scheme 从列表中选择7。
我试图从列表中选出7个,以下是我的代码:Scheme 从列表中选择7。,scheme,racket,Scheme,Racket,我试图从列表中选出7个,以下是我的代码: (define (pick7 x) (cond ((null? x) x) ((= (car x) 7) pick7 (cdr x)) (else (cons (car x) (pick7 (cdr x)))))) 但是当我调用(pick7(13(57)9))时,它会给我一个错误。我想我看到了问题所在,(car x)并不总是一个数字,所以我需要将其分解 如何解决这个问题?从列表中选择7是什么意思?你是说把它拿走?如果
(define (pick7 x)
(cond ((null? x) x)
((= (car x) 7) pick7 (cdr x))
(else (cons (car x) (pick7 (cdr x))))))
但是当我调用(pick7(13(57)9))
时,它会给我一个错误。我想我看到了问题所在,(car x)
并不总是一个数字,所以我需要将其分解
如何解决这个问题?从列表中选择7是什么意思?你是说把它拿走?如果是这样,您还需要检查第一个元素是否是列表
(define (pick7 x)
(cond ((null? x) x)
((<??>) (cons (pick7 (car x)) (pick7 (cdr x))))
((= (car x) 7) (pick7 (cdr x)) ; missing paren
(else (cons (car x) (pick7 (cdr x))))))
(定义(pick7 x)
(条件((空?x)x)
(()(cons(pick7(car x))(pick7(cdr x)))
(=(车x)7)(pick7(cdr x));缺少参数
(其他(cons(车辆x)(pick7(cdr x()()))))
从列表中选择7是什么意思?是指删除它吗?如果是,您还需要检查第一个元素是否是列表
(define (pick7 x)
(cond ((null? x) x)
((<??>) (cons (pick7 (car x)) (pick7 (cdr x))))
((= (car x) 7) (pick7 (cdr x)) ; missing paren
(else (cons (car x) (pick7 (cdr x))))))
(定义(pick7 x)
(条件((空?x)x)
(()(cons(pick7(car x))(pick7(cdr x)))
(=(车x)7)(pick7(cdr x));缺少参数
(其他(cons(车辆x)(pick7(cdr x()()))))
如果“拾取”的意思是从列表中“删除”元素,那么对于任意嵌套列表的列表,请填写以下空白:
(define (pick7 x)
(cond (<???> <???>) ; if the list is null, return null
((not (pair? <???>)) ; if the first element is not a list
(if <???> ; if the first element is 7
(pick7 <???>) ; advance recursion over rest of list
(cons <???> ; else cons the first element and
(pick7 <???>)))) ; advance recursion over rest of list
(else (cons ; if the first element is a list, then cons
(pick7 <???>) ; the recursion over the first element
(pick7 <???>))))) ; and the recursion over the rest of list
如果“拾取”的意思是从列表中“移除”元素,那么对于任意嵌套的列表,您可以这样做,请填写以下空白:
(define (pick7 x)
(cond (<???> <???>) ; if the list is null, return null
((not (pair? <???>)) ; if the first element is not a list
(if <???> ; if the first element is 7
(pick7 <???>) ; advance recursion over rest of list
(cons <???> ; else cons the first element and
(pick7 <???>)))) ; advance recursion over rest of list
(else (cons ; if the first element is a list, then cons
(pick7 <???>) ; the recursion over the first element
(pick7 <???>))))) ; and the recursion over the rest of list
(=(car x)7)pick7(cdr x))
没有做你认为它能做的事;p(提示:缺少括号)什么是“挑选”?你想从列表中删除所有的7吗?(=(car x)7)pick7(cdr x))
没有做你认为它能做的事;p(提示:缺少括号)什么是“挑选”?是否要从列表列表中删除所有7?