我想要做的是創建一個函數,該函數接受一個值列表和一個字符列表併合並相應的字符(「原子」,我認爲他們會在技術上被調用)到一個新的列表中。使用scheme/racket從列表中返回特定項目
這是我到目前爲止;
#lang racket
(define (find num char)
(if (= num 1)
(car char) ;Problem here perhaps?
(find (- num 1) (cdr char))))
(define (test num char)
(if (null? num)
'("Done")
(list (find (car num) (test (cdr num) char)))))
然而,這給了我一個錯誤,這在大多數情況下我明白它是什麼說法,但我不明白什麼是錯的創建錯誤。考慮下面這個簡單的測試輸入,這是我得到
> (test '(2 1) '(a b c))
car: contract violation
expected: pair?
given: '()
從本質上講,輸出應爲'(b a)
,而不是錯誤明顯。
一個新的計劃用戶的一些幫助和指導,將不勝感激!
編輯:
這是我能夠運行的代碼。
#lang racket
(define (find num char)
(cond ((empty? char) #f)
((= num 1) (car char))
(else (find (- num 1) (cdr char)))))
(define (project num char)
(if (empty? num)
'()
(cons (find (car num) char) (project (cdr num) char))))
編輯完成後,'project'幾乎是正確的!只是基本情況是錯誤的:正確的返回值是''()',而不是'(list'())' –
耶對不起,我意識到我可能在你評論之前幾秒鐘就這樣做了。它改變了。 –