微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Scheme 中使用 char 的条件

如何解决在 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。