在 Racket 中的 cond 中打印多个语句

发布于 2024-11-04 08:45:06 字数 237 浏览 3 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

献世佛 2024-11-11 08:45:06

你说“返回/打印”,但这是两个非常不同的事情:

  • 如果你想要返回的东西,那么你应该使用多个值,例如 (值 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 (or display, or write, or printf etc etc).

If you're a complete newbie, then it's likely that neither of these is the right solution for you.

久隐师 2024-11-11 08:45:06

好吧,您并没有真正“打印”任何内容,只是返回最后一个表达式(在本例中为 `(bye ,exp) )。如果您想打印它们,请使用 display

(cond ((number? 2)
       (display `(hi ,var))
       (display `(bye ,exp))))

Well, you're not really "printing" anything, just returning the last expression (`(bye ,exp) in this case). If you want to print them, use display:

(cond ((number? 2)
       (display `(hi ,var))
       (display `(bye ,exp))))
旧街凉风 2024-11-11 08:45:06

目前尚不清楚OP是否想要返回数据,或者打印数据。

扩展 Eli Barzilay 的返回列表的建议,最简单的修改就是简单地在两个表达式上计算list

(let ((var `(make))
      (exp '(1 2)))
   (cond
      [(number? 2) (list `(hi ,var)
                         `(bye ,exp))]))

这会返回

'((hi (make)) (bye (1 2)))

,但不打印 任何东西。另请注意,逗号 (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:

(let ((var `(make))
      (exp '(1 2)))
   (cond
      [(number? 2) (list `(hi ,var)
                         `(bye ,exp))]))

This returns

'((hi (make)) (bye (1 2)))

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文