计划连接字符串以显示
Scheme concatenate strings to display
我正在使用英文版 DrRacket 6.4 在 Scheme 中创建一个小应用程序。
我想知道是否有更有效的方法来连接以下代码。
[它有效我只是不确定它是否是最干净的,因为我是 Scheme 的新手]
(display "Rolling ")
(display (number->string (- 5 (length my-rolled-dice))))
(display " dice\n")
(display "You rolled\n")
(define my-roll (make-list-of-random-numbers (- 5 (length my-rolled-dice) ) 6))
(display my-roll)
(display "\n")
我正在寻找屏幕上的以下输出
Rolling 5 dice
You rolled
(3 1 3 6 6)
是否有更简洁的方式来编写它,或者它是否像在 Scheme 中一样干净?
使用printf
,更短:
(printf "Rolling ~a dice~n" (- 5 (length my-rolled-dice)))
(printf "You rolled~n~a" (make-list-of-random-numbers (- 5 (length my-rolled-dice)) 6))
在Racket中,printf
有效,但在Scheme中,像MIT-Scheme一样,没有printf
。在这种情况下,您可以使用 map
和 display
。例如:
(map display
(list "Rolling " (- 5 (length my-rolled-dice))
" dice\nYou rolled\n" my-roll "\n"))
我正在使用英文版 DrRacket 6.4 在 Scheme 中创建一个小应用程序。 我想知道是否有更有效的方法来连接以下代码。 [它有效我只是不确定它是否是最干净的,因为我是 Scheme 的新手]
(display "Rolling ")
(display (number->string (- 5 (length my-rolled-dice))))
(display " dice\n")
(display "You rolled\n")
(define my-roll (make-list-of-random-numbers (- 5 (length my-rolled-dice) ) 6))
(display my-roll)
(display "\n")
我正在寻找屏幕上的以下输出
Rolling 5 dice
You rolled
(3 1 3 6 6)
是否有更简洁的方式来编写它,或者它是否像在 Scheme 中一样干净?
使用printf
,更短:
(printf "Rolling ~a dice~n" (- 5 (length my-rolled-dice)))
(printf "You rolled~n~a" (make-list-of-random-numbers (- 5 (length my-rolled-dice)) 6))
在Racket中,printf
有效,但在Scheme中,像MIT-Scheme一样,没有printf
。在这种情况下,您可以使用 map
和 display
。例如:
(map display
(list "Rolling " (- 5 (length my-rolled-dice))
" dice\nYou rolled\n" my-roll "\n"))