Scheme - 如何读取 cond 中的字符串

Scheme - How to read the string in cond

我只想知道是否可以在 Scheme 中读取字符串,例如 code == "SST"

这是我的代码:
(display "Enter the Code")
(define code(read))

(display "Quantity:")
(define qty(read))

(cond
((= code 1)(define price 20.00))只能读取数字
((= code LST)(define price 25.00)) 我试过了,但没有任何反应
(else 0)
)

(define total(* price qty))
(display "Total Price:")
(display total)

如果用户输入“SST”,它将等于价格 20.00,而“LST”等于价格 25.00
我使用 Repl.it 作为编译器。

read 不读取字符串或字节。它读取 Scheme 代码并将 returns Scheme 代码作为数据。例如。如果您输入 "SST",它将变成字符串 "SST",但是如果您输入 SST,它将读入符号 SST.

= 仅适用于数字。如果您尝试将非数字与 = 进行比较,程序可能会停止。您可以使用 equal? 来比较看起来应该相同的东西,它也适用于数字。

define 不能在任何地方。如果你把它放在顶层,它将成为全局变量,如果你把它放在 letprocedures 之类的地方,它们将成为本地绑定。周四你有两个可能的解决方案:

;; make cond the expression
(define price 
  (cond 
    ((equal? code 1) 20.0)
    ((equal? code 'LST) 25.0)
    (else 0)))

;; define it once, set! it from other places
(define price 0)
(cond 
  ((equal? code 1) (set! price 20.0))
  ((equal? code 'LST) (set! price 25.0)))