在 Racket 中的 cond 中打印多个语句
在 Racket 中,我尝试在“cond”语句中打印多个表达式,如下所示,
(let ((var `(make))
(exp '(1 2)))
(cond
[(number? 2) `(hi ,var)
`(bye ,exp)]))
但只有“bye”语句返回/打印在屏幕上。第一个“hi”根本不被评估。我如何退回/打印两者?
In Racket, I am trying to print multiple expressions in "cond" statement as below,
(let ((var `(make))
(exp '(1 2)))
(cond
[(number? 2) `(hi ,var)
`(bye ,exp)]))
But only the "bye" statement is returned/printed on the screen.The first "hi" is not evalauted at all. How do I return/print both ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你说“返回/打印”,但这是两个非常不同的事情:
如果你想要返回的东西,那么你应该使用多个值,例如
(值 1 2)
(或者,如果您不知道多个值,您可以返回一个包含两个值的列表,作为一种廉价的黑客手段)。如果您想打印内容,请使用
打印
两次(或显示
,或写
,或printf
等等)。如果您是一个完全的新手,那么很可能这些都不是适合您的解决方案。
You say "returned/printed" but those are two very different things:
If you want to things returned, then you should use multiple values, like
(values 1 2)
(or you can return a list with the two values as a cheap hack in case you don't know about multiple values).If you want to print stuff, then use
print
twice (ordisplay
, orwrite
, orprintf
etc etc).If you're a complete newbie, then it's likely that neither of these is the right solution for you.
好吧,您并没有真正“打印”任何内容,只是返回最后一个表达式(在本例中为
`(bye ,exp)
)。如果您想打印它们,请使用display
:Well, you're not really "printing" anything, just returning the last expression (
`(bye ,exp)
in this case). If you want to print them, usedisplay
:目前尚不清楚OP是否想要返回数据,或者打印数据。
扩展 Eli Barzilay 的返回列表的建议,最简单的修改就是简单地在两个表达式上计算
list
:这会返回
,但不打印 任何东西。另请注意,逗号 (unquote) 现在会导致局部变量 var 和 exp ,待评价。
Chris Jester-Young 的回答展示了如何打印求值的表达式,同时不返回任何内容。
It is not clear whether the OP wants to return the data, or print them.
Expanding on Eli Barzilay's suggestion to return a list, the simplest modification would be simply to evaluate
list
on the two expressions:This returns
but does not print anything. Notice also that the comma (unquote) now causes both local variables, var and exp, to be evaluated.
Chris Jester-Young's answer shows how to print the evaluated expressions, while returning nothing.