如何将符号 a 与字符 a 进行比较?

And*_*iau 3 scheme

我有一个包含字母的列表。当我做 (car '(a)) 时,它给了我符号 a。我如何将它与字符 a 进行比较?

我必须做 (eq? (car list) (car '(a)) 吗?

Joh*_*nts 5

符号和字符是不同种类的数据。幸运的是,Scheme 愿意让你转换几乎任何你想要的东西。在 Racket 中,例如:

#lang racket

;; the symbol a:
'a

;; the character a:
#\a

;; are they equal? no.
(equal? 'a #\a) ;; produces #f

;; converting a character to a symbol:
(define (char->symbol ch)
  (string->symbol (string ch)))

(char->symbol #\a) ;;=> produces 'a

;; converting a symbol to a character
(define (symbol->char sym)
  (match (string->list (symbol->string sym))
    [(list ch) ch]
    [other (error 'symbol->char 
                  "expected a one-character symbol, got: ~s" sym)])) 

(symbol->char 'a) ;; => produces #\a   
Run Code Online (Sandbox Code Playgroud)

综上所述,如果您正在做家庭作业,那么老师几乎肯定会为您准备一条更简单的路径。