将参数添加到列表会导致错误
我尝试了一个示例,其中我们需要传递一个列表作为参数,如果条件成功,我想将结果添加到新列表中。
代码如下:
(define get-description
(lambda (codeValue newList)
(cond
((= (car codeValue) 1) (cons "A" newlist))
((= (car codeValue) 2)(cons "B" newlist))
((= (car codeValue) 3) "C")
(else "Negative numbers are not valid"))))
我将其作为函数调用传递:
(get-description (list 1 2 3) (list))
我得到输出:
(cons "A" empty)
输出应仅显示: (A)
我正在使用 DrRacket 编写程序,并选择语言模式为:初级学生。
为什么我的 newlist< 中会出现
cons
和 A
以及 ""
和 empty
/代码>?
I tried an example where we need to pass a list as arguments and if condition succeeds I want to add result to a new list.
Here's the code:
(define get-description
(lambda (codeValue newList)
(cond
((= (car codeValue) 1) (cons "A" newlist))
((= (car codeValue) 2)(cons "B" newlist))
((= (car codeValue) 3) "C")
(else "Negative numbers are not valid"))))
I pass this as the function call:
(get-description (list 1 2 3) (list))
I get output:
(cons "A" empty)
Output should just show: (A)
I am using DrRacket for writing my programs and have chosen language mode as: Beginning Student.
Why do I get cons
and A
with ""
and empty
in my newlist
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请不要在 Racket 中使用“Beginning Student”作为语言类型。这是专门为 HtDP 书籍制作的子集。语言“racket”、“r5rs”、“pretty big”更像是真正的计划,并且应该都适用于“小阴谋家”。
在您的参数列表中,您有 (codeValue newList),但在程序正文中您引用 newlist。我使用的所有方案都区分大小写。将
newList
更改为newlist
使您的程序在 Chez Scheme 和 Guile 上也能完美运行。编辑:澄清一下,
“A”
是一个字符串。 Scheme 还具有附加数据类型 symbol,它只是一个名称,没有其他内容(可能就是您想要的)。如果您期望(A)
,您可能需要(cons 'A newlist)
而不是(cons "A" newlist)
。Please don't use "Beginning Student" as a language type in Racket. That's a subset specially made for the HtDP book. The languages "racket", "r5rs", "pretty big", are more like real Schemes and should all work for The Little Schemer.
In your arguments list, you have (codeValue newList), but in the program body you refer to newlist. All of the Schemes that I've used are case-sensitive. Changing your
newList
tonewlist
made your program run perfectly fine on Chez Scheme and Guile too.Edit: To clarify,
"A"
is a string. Scheme also has the additional data type of symbol, which is just a name and nothing else (and is probably what you want here). You probably want to(cons 'A newlist)
rather than(cons "A" newlist)
if you're expecting(A)
.其他方案只会打印
("A")
。这样的输出显然是 Racket 语言的特性。至于为什么
A
要加引号,那是因为它是一个字符串对象,而字符串对象就是这样打印的。但如果你要显示这样一个对象,你会因为它的孤独而得到A
。Other Schemes would print just
("A")
. Such output is clearly an idiosyncrasy of the Racket language.As for why the
A
is in quotation marks, that's because it's a string object, and that's simply how string objects are printed. But if you were to DISPLAY such an object, you'd get theA
by its lonesome.