您好,我正在写一些东西,在第一个元素之后每隔三个元素抓取一次。但是由于“;对象(a b c d e f g)不适用”,我无法测试逻辑。代码如下,原子?检查它是否是一个列表,listful 被定义为一个空列表。
(DEFINE (threes var)
(if(atom? var)
((newline) (DISPLAY "Bad input") ))
(APPEND(listful (CAR var)))
(if(> 3 (length var))
(threes (cdddr(listful)))
(listful))
)
谁能给我一些提示?以下是我在 Scheme 环境中调用方法的方式。
>(threes (list1))
>(threes '(A B C D E F))
if
只能有一个表达式作为后件,一个表达式作为替代项。如果您需要多个,则必须使用 begin
来包含表达式序列 - 用 ()
包围表达式是行不通的,这会导致报告错误,因为 Scheme 将 ()
解释为函数应用程序。这将是正确的语法:
(define (threes var)
(if (atom? var)
(begin
(newline)
(display "Bad input")))
(append (listful (car var)))
(if (> 3 (length var))
(begin
(threes (cdddr (listful)))
(listful))))
... 然而,这不太可能奏效。最近几天你想做什么被问了几次,特别是here是我自己之前的回答:
(define (threes lst)
(cond ((or (null? lst) (null? (cdr lst))) lst)
((null? (cdr (cdr lst))) (list (car lst)))
(else (cons (car lst)
(threes (cdr (cdr (cdr lst))))))))
例如:
(threes '(a b c d e f g h i j k l m n o p))
=> '(a d g j m p)
参见 original question对于其他解决问题的方法,有详细的解释。