如何读取字符串以获取 GNU Guile 中的用户输入?
How to read a string to get user input in GNU Guile?
我正在尝试制作剪刀石头布游戏来帮助自己学习 GNU Guile。我在获取用户输入(即玩家在游戏中的选择)时遇到了障碍。如果我将它设置为一个字符串,那么游戏就可以正常运行。如果我使用 (read)
我会从查找中返回 #f 作为类型。我尝试格式化 read 以使其成为字符串,但没有用。
(define (print a)
(display a)
(newline))
(define choices (make-hash-table 3))
(hashq-set! choices "r" "s")
(hashq-set! choices "s" "p")
(hashq-set! choices "p" "r")
(define (cpu-choice) (list-ref (list "r" "p" "s") (random 3)))
(print "You are playing rock paper scissors.")
(print "Type r for rock, p for paper, and s for scissors.")
(define draw
;; "s" ; This works as a test.
(read (open-input-string (read))) ; Can't get user in as string, so the hashq-ref will work.
)
(define cpu-draw (cpu-choice))
;; debug
(print (format #f "Player enterd ~a" draw))
(print (format #f "Player needs to with ~a" (hashq-ref choices draw))) ; Keeps coming back as #f
(print (format #f "CPU has entered ~a" cpu-draw))
;; norm
(newline)
(when (eq? draw cpu-draw)
(print "There was a tie")
(exit))
(when (eq? (hashq-ref choices draw) cpu-draw)
(print "You have won.")
(exit))
(print "You have failed. The computer won.")
如何从用户那里得到一个字符串?可能类似于 (str (read))
或 (read-string)
(读取为字符串)。
$ guile --version
guile (GNU Guile) 2.0.13
更新
我只想提一下,虽然批准的答案是正确的,但我在写这篇文章时不明白 Guile/Scheme 是如何处理字符串和符号的。让程序运行的唯一方法是将 choices
和 cpu-choice
列表中的所有字符串更改为符号。例如:
(hashq-set! choices 'r 's)
(list 'r 'p 's)
谢谢 Óscar López 的帮助。
除非您用双引号将输入括起来,否则您键入的值将被解释为 符号 。试试这个:
(define str (read))
> "hello"
或者这样:
(define str (symbol->string (read)))
> hello
无论如何,str
现在将保存一个实际的字符串:
str
=> "hello"
我正在尝试制作剪刀石头布游戏来帮助自己学习 GNU Guile。我在获取用户输入(即玩家在游戏中的选择)时遇到了障碍。如果我将它设置为一个字符串,那么游戏就可以正常运行。如果我使用 (read)
我会从查找中返回 #f 作为类型。我尝试格式化 read 以使其成为字符串,但没有用。
(define (print a)
(display a)
(newline))
(define choices (make-hash-table 3))
(hashq-set! choices "r" "s")
(hashq-set! choices "s" "p")
(hashq-set! choices "p" "r")
(define (cpu-choice) (list-ref (list "r" "p" "s") (random 3)))
(print "You are playing rock paper scissors.")
(print "Type r for rock, p for paper, and s for scissors.")
(define draw
;; "s" ; This works as a test.
(read (open-input-string (read))) ; Can't get user in as string, so the hashq-ref will work.
)
(define cpu-draw (cpu-choice))
;; debug
(print (format #f "Player enterd ~a" draw))
(print (format #f "Player needs to with ~a" (hashq-ref choices draw))) ; Keeps coming back as #f
(print (format #f "CPU has entered ~a" cpu-draw))
;; norm
(newline)
(when (eq? draw cpu-draw)
(print "There was a tie")
(exit))
(when (eq? (hashq-ref choices draw) cpu-draw)
(print "You have won.")
(exit))
(print "You have failed. The computer won.")
如何从用户那里得到一个字符串?可能类似于 (str (read))
或 (read-string)
(读取为字符串)。
$ guile --version
guile (GNU Guile) 2.0.13
更新
我只想提一下,虽然批准的答案是正确的,但我在写这篇文章时不明白 Guile/Scheme 是如何处理字符串和符号的。让程序运行的唯一方法是将 choices
和 cpu-choice
列表中的所有字符串更改为符号。例如:
(hashq-set! choices 'r 's)
(list 'r 'p 's)
谢谢 Óscar López 的帮助。
除非您用双引号将输入括起来,否则您键入的值将被解释为 符号 。试试这个:
(define str (read))
> "hello"
或者这样:
(define str (symbol->string (read)))
> hello
无论如何,str
现在将保存一个实际的字符串:
str
=> "hello"