[英]Scheme Character Comparison
假設我有一個這樣的列表:
(define test '(r x -))
我想知道如何區分列表中的每個值,例如:
(define (distinguish test) (equal? (car test) r))
->當然,這會返回錯誤,但是我希望它返回#t或類似的東西。
謝謝您的幫助!
代碼中未引用的符號是變量
(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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.