方案:三个点元素的列表奇怪地返回(像中缀运算符?)
我是一名新的计划/球拍学生,所以请原谅任何明显的语法错误。
今天在课堂上提到方案列表 '(a, b, c)
应该是无效的,但是当我们运行它时,它返回:
>'(a . b . c)
(b a c)
这没有任何意义。 Afaik,解释器应该创建一个带有 car 'a 和 cdr 'b 的 cons 单元,并且 'c 应该无效。也就是说,口译员在这里做了一些非常奇怪的事情。这适用于#lang 方案、#lang 球拍等。我们使用 DrRacket 作为解释器。
有趣的是,
>'(a . b . c . d)
抛出异常并死亡。
我很好奇,并且很想能够理解这一点,因为我是这门语言的新手。谷歌非常没有帮助(可能是因为搜索词有点含糊)谢谢!
编辑: 这可能是因为 '(a . b . c)
被解释为 b 作为中缀运算符。例如: >(4 . + . 6)
返回 10。也许解释器将 b 像运算符一样使用?即 (bac)
类似于 (+ 4 6)
,中缀方式。
实验说:
>(define b +)
>(define a 4)
>(define c 6)
>(a . b . c)
10
所以我认为这解决了问题,但我仍然没有完全理解“.”的用法。在这种情况下,操作员。我认为我们已经解决了这个问题,但任何更多的见解将不胜感激!
I am a new Scheme/Racket student, so please excuse any blatant syntax errors.
It came up in class today that the scheme list '(a, b, c)
should be invalid, but when we ran it, it returned:
>'(a . b . c)
(b a c)
Which makes no sense. Afaik, the interpreter should create a cons cell with car 'a and cdr 'b, and the 'c should be invalid. That said, the interpreter is doing something really strange here. This works with #lang scheme, #lang racket, and others. We are using DrRacket as the interpreter.
Interestingly,
>'(a . b . c . d)
throws an exception and dies.
I am very curious and would love to be able to understand this since I am new to the language. Google was very unhelpful (probably since the search terms are kind of ambiguous) Thank you!
EDIT:
It might be because '(a . b . c)
is interpreted with b as an infix operator. For example: >(4 . + . 6)
returns 10. Perhaps the interpreter is using b like an operator? i.e. (b a c)
like (+ 4 6)
, infix-wise.
Expermentation says:
>(define b +)
>(define a 4)
>(define c 6)
>(a . b . c)
10
So I think this solves the problem, but I still don't fully understand the use of the "." operator in this case. I think we've solved this, but any more insight would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短的回答:你明白了。有关特定于 Racket 的点的使用的更多信息,请参阅 中缀。
Short answer: you got it. For more information on this Racket-specific use of dots, see the documentation for infix in the Racket docs.
这是 Racket 阅读器的一个特色功能。 (请参阅约翰的回答。)
对于其他实现,您可以使用 可读 S 表达式 阅读器能够读取中缀表达式。它使用花括号。例如,
{3 + 4}
读入为(+ 3 4)
。更特别的是(比 Racket 的中缀阅读器),您可以使用{3 + 4 + 5}
或{3 + 4 + 5 + 6}
;它们将分别读取为(+ 3 4 5)
和(+ 3 4 5 6)
。It's a special feature of Racket's reader. (See John's answer.)
For other implementations, you can instead use the readable S-expressions reader to be able to read infix expressions. It uses curly braces. e.g.,
{3 + 4}
is read in as(+ 3 4)
. Even more special (than Racket's infix reader), you can use{3 + 4 + 5}
or{3 + 4 + 5 + 6}
; they will read as(+ 3 4 5)
and(+ 3 4 5 6)
respectively.