2014-04-10 40 views
0

嗨我正在寫東西,抓住第一個元素後的每第三個元素。但是我無法測試邏輯,因爲「;對象(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)) 

回答

1

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其他方式來解決問題,並有詳細的解釋。

相關問題