方案中没有空单词的空列表

发布于 2024-11-10 06:23:13 字数 158 浏览 2 评论 0原文

我正在编写一个递归函数,在基本情况下返回一个空列表。然而,函数的输出在我的列表中显示“空”单词,这是我不想要的。像这样;

(列表(列表'abc)(列表'def)空(列表'ghi))

我怎样才能防止这种情况?谢谢。

I am writing a recursion function returning an empty list in the base case. However the output of functions shows "empty" word in the my list, which I don't want.Like this;

(list (list 'abc) (list 'def) empty (list 'ghi))

How can I prevent this? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

秋叶绚丽 2024-11-17 06:23:13

问题可能是因为 Racket 有多种值的打印样式。尝试将其(在语言选择对话框中)更改为“write”或任何名称,这应该使其输出 ((abc) (def) () (ghi))

The problem is probably because Racket has several printing styles for values. Try changing it (in the language selection dialog) to "write" or whatever it's called, which should make it output ((abc) (def) () (ghi)) instead.

夏花。依旧 2024-11-17 06:23:13

您在结果中看到的 empty 不是一个“单词”——请注意,它没有被引用。如果您确实期望结果中出现一个空列表,那么看起来您已经得到了一个。您甚至可以检查:

> (empty? (third (list (list 'abc) (list 'def) empty (list 'ghi))))
#t

The empty that you see in the result is not a "word" -- note that it's not quoted. If you do expect an empty list in the result, then it looks like you got one. You can even check for that:

> (empty? (third (list (list 'abc) (list 'def) empty (list 'ghi))))
#t
别念他 2024-11-17 06:23:13

在不了解详细信息的情况下,我最好的猜测是

(let ((result (recursive-call ...)))
(if (null? result) (resursive-call (cdr whatever-list))
(cons result (cdr whatever-list)))

本质上,只需检查结果是否为空列表,如果是,则不要将其放入您要返回的列表中。

Without knowing details, my best guess would be something like

(let ((result (recursive-call ...)))
(if (null? result) (resursive-call (cdr whatever-list))
(cons result (cdr whatever-list)))

Essentially, just check if the result is the empty list, and if so, don't put it into the list that you're returning.

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