在 Scheme 中使用 char 的条件

问题描述

我试图在 if-else 语句中使用 char 作为条件,但该条件似乎只能接受整数输入。

这是我的代码

(display "Presenter - R \nParticipant - P")

(define (option)
  (define r(read))
(cond
  ((= r R )
   (display "Presenter: \nLocal - RM1272\nInternational - RM1474"))
  ((= r P )
   (display "Participant: \nLocal - RM795\nInternational - RM800"))
))

输出

Presenter - R 
Participant - P
> (option)
R
. . =: contract violation
  expected: number?
  given: r 

解决方法

你的 (read) 的结果是符号 R,它既不是字符也不是标识符。
您也可以只使用 = 来比较数字。

> (define r (read))
R
> r
'R
> (equal? r R)
. . R: undefined;
 cannot reference an identifier before its definition
> (equal? r #\R)
#f
> (equal? r 'R)
#t