方案特征比较

时间:2017-05-26 00:07:16

标签: list scheme character

假设我有一个像这样的列表:

(define test '(r x -))

我想知道如何区分列表中的每个值,例如:

(define (distinguish test) (equal? (car test) r)) - >当然这会返回错误,但我希望它返回#t或类似的东西。

感谢您的帮助!

1 个答案:

答案 0 :(得分: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)