2017-04-17 98 views
0

我試圖使用與字符串通過包含數字,字符串和空列表清單,遞歸解析和轉換任何字符串到數字的名單上使用與字符串>號碼>數包含字符串和數字

(define tnode '(5 "5"()())) 

使用功能

(define (test funct inlist) 
    (cond((null? inlist) '()) 
     ((number? inlist) ((cons (car inlist) (test funct (cdr inlist))))) 
     (else (cons (funct (car inlist)) (test funct (cdr inlist)))) 
     ) 
    ) 

(test string->number tnode) 

但是我recieving合同VI第一個數字5(如果我使用只有字符串和空列表的tnode,則在後面的空列表上)出現olation錯誤。看起來好像該函數忽略了前兩個條件並直接轉到else語句。爲什麼是這樣?我不認爲有任何語法錯誤,因爲其他測試過的cond函數工作正常。我不太確定問題在哪。

回答

1

inlist是整個列表;你想要檢查它的第一個元素,就像處理結果中的第一個元素一樣。

+0

謝謝你,與周圍但是空列表沒有出現的數字的工作有幫助。根據需要,我得到'(5 5)'而不是'(5 5()()'的輸出。 –

+0

我發現問題在於,我需要(null?inlist)的條件和(null?(car inlist))的條件,因爲需要有一個最終的null值, 最終代碼將在另一個答案中 –

0

這給了我所期望的輸出的功能可按代碼如下

(define (test funct inlist) 
    (cond((null? inlist) '()) 
     ((null? (car inlist)) (cons '() (test funct (cdr inlist)))) 
     ((number? (car inlist)) (cons (car inlist) (test funct (cdr inlist)))) 
     (else (cons (funct (car inlist)) (test funct (cdr inlist)))) 
     ) 
    )