[英]Scheme: Lists of three dotted elements returning strangely (like an infix operator?)
I am a new Scheme/Racket student, so please excuse any blatant syntax errors. 我是一名新的Scheme / Racket学生,请原谅任何明显的语法错误。
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)
应该是无效的,但是当我们运行它时,它返回:
>'(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.
Afaik,解释器应该创建一个带有'a和cdr'b的cons单元格,'c应该是无效的。 That said, the interpreter is doing something really strange here.
也就是说,翻译在这里做了一些非常奇怪的事情。 This works with #lang scheme, #lang racket, and others.
这适用于#lang方案,#lang racket等。 We are using DrRacket as the interpreter.
我们使用DrRacket作为翻译。
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. 编辑:可能是因为
'(a . b . c)
被解释为b作为中缀运算符。 For example: >(4 . + . 6)
returns 10. Perhaps the interpreter is using b like an operator? 例如:
>(4 . + . 6)
返回10.也许解释器使用b像运算符? ie (bac)
like (+ 4 6)
, infix-wise. 即
(bac)
像(+ 4 6)
,中缀。
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!
我想我们已经解决了这个问题,但我们将非常感谢您的见解!
It's a special feature of Racket's reader. 这是Racket读者的一个特色。 (See John's answer.)
(见约翰的回答。)
For other implementations, you can instead use the readable S-expressions reader to be able to read infix expressions. 对于其他实现,您可以使用可读的S表达式读取器来读取中缀表达式。 It uses curly braces.
它使用花括号。 eg,
{3 + 4}
is read in as (+ 3 4)
. 例如,
{3 + 4}
被读入(+ 3 4)
。 Even more special (than Racket's infix reader), you can use {3 + 4 + 5}
or {3 + 4 + 5 + 6}
; 甚至更特殊(比Racket的中缀阅读器),你可以使用
{3 + 4 + 5}
或{3 + 4 + 5 + 6}
; they will read as (+ 3 4 5)
and (+ 3 4 5 6)
respectively. 它们分别为
(+ 3 4 5)
和(+ 3 4 5 6)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.