将参数添加到列表会导致错误

发布于 2024-10-01 16:41:43 字数 708 浏览 3 评论 0原文

我尝试了一个示例,其中我们需要传递一个列表作为参数,如果条件成功,我想将结果添加到新列表中。

代码如下:

(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< 中会出现 consA 以及 ""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 技术交流群。

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

发布评论

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

评论(2

原来分手还会想你 2024-10-08 16:41:43

请不要在 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 to newlist 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).

想挽留 2024-10-08 16:41:43

其他方案只会打印 ("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 the A by its lonesome.

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