2017-05-26 115 views
0

比方說,我有這樣一個清單:方案特徵比較

(define test '(r x -)) 

我想知道我怎麼能區分每個值的列表中,爲的exaple:

(define (distinguish test) (equal? (car test) r)) - >中當然這會返回錯誤,但我希望它返回#t或類似的東西。

感謝您的幫助!

+0

你問的是如何訪問符號'r','x' a nd' -',如'car','cadr'和'caddr'? – Sylwester

+0

不,我在問怎麼才能得到'正確'的操作(等於?(汽車測試)r)就好像它在比較** r **和** r **一樣,因爲它實際上返回'Error' –

+1

你應該寫:'(define(區分測試)(相等?(汽車測試)'r))'。 – Renzo

回答

2

在代碼不引用符號是可變

(define r 'x)  ; define the variable r to be the symbol x 
(eq? (car test) r) ; ==> #f (compares the symbol r with symbol x) 
(eq? (cadr test) r) ; ==> #t (compares the symbol x with the symbol x) 
(eq? (car test) 'r) ; ==> #t (compares the symbol r with the symbol r) 

中的符號列表比較

(define test-list '(fi fa foo)) 
(define test-symbol 'fi) 
(eq? (car test-list) test-symbol) ; ==> #t (compares fi with fi) 
(eq? 'fi 'fi)      ; ==> #t (compares fi with fi) 

字在字符串比較(該問題的標題是有關字符不是符號):

(define test-string "test") 
(define test-char #\t) 
(eqv? (string-ref test-string 0) test-char) ; ==> #t (compares #\t with #\t) 
(eqv? #\t #\t)        ; ==> #t (compares #\t with #\t) 
+0

謝謝!你的回答也很有幫助。 –