什么是' (撇号)在 Lisp/Scheme 中?
我正在自学计划的第一天第一个小时。不用说,我什么也不明白。所以我正在阅读《小阴谋家》并使用这个东西:
作为解释器。
例如,我需要使用 '
(atom? 'turkey)
以避免“未定义的变量”错误。根据这本书,'
是 Common Lisp 的东西。
我有两个问题:
我上面提到的翻译是一个好的翻译吗?你能推荐另一个吗?我需要一个能够与小阴谋家配合使用的翻译器。
什么是
'
?
I am on day 1 hour 1 of teaching myself Scheme. Needless to say, I don't understand anything. So I'm reading The Little Schemer and using this thing:
as an interpreter.
I need to use '
in for example
(atom? 'turkey)
to avoid an "undefined variable" error. The '
, according to the book, is a Common Lisp thing.
I have two questions:
Is the interpreter I mentioned above a good one? Can you recommend another? I need one that will go well with The Little Schemer.
What is
'
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我建议你转移到一个更好的环境,比如PLT Scheme,它有一个IDE,调试器和许多库。当您继续前进并开始编写更大的程序时,您将需要它们。
单引号字符是“quote”表达式的语法糖,因此 'turkey 与 (quote turkey) 相同。基本上,“引用”的作用是关闭方案评估器。换句话说,“quote”逐字返回表达式。如果没有“quote”,那么Scheme会尝试在当前环境下评估“turkey”。这不是 Common Lisp 的东西,而是 Lisp 的东西。 Common Lisp 和Scheme 是Lisp 的两种方言。所有 Lisp 教程/书籍中都解释了“quote”的用法。另请参阅此问题的答案。
I suggest that you move to a better environment like PLT Scheme, which has an IDE, debugger and lots of libraries. As you move forward and start writing larger programs, you will need them.
The single-quote character is syntactic sugar for the "quote" expression, so 'turkey is the same as (quote turkey). Basically, what "quote" does is to switch off the Scheme evaluator. In other words, "quote" returns the expression, verbatim. If there was no "quote", then Scheme would try to evaluate "turkey" in the current environment. This is not a Common Lisp thing but a Lisp thing. Common Lisp and Scheme are two dialects of Lisp. The uses of "quote" are explained in all Lisp tutorials/books. Also see the answers to this question.
如果您正在寻找
scheme
的最佳 IDE,那么请选择Racket
。但是当启动Racket博士
时第一行应该是
#lang schema
,因为Racket
有很多语言,我们已经明确提到了我们将使用哪种语言。当我们想要传递参数本身而不是传递参数的值时,我们可以使用 quote。它主要与使用列表、对和原子期间的过程传递有关,这些在 C 编程语言中不可用(大多数人开始使用 C 编程进行编程,因此我们会感到困惑)这是Scheme编程语言中的代码,它是 Lisp 的一种方言我想你能理解这段代码。
最后一行 (atom? 'abc) 传递 abc ,因为它是检查 abc 是否是原子的过程,但是当您传递 (atom? abc) 时,它会检查 abc 的值并将该值传递给它。因为,我们没有为其提供任何价值
If you looking for a best IDE for
scheme
then go forDr Racket
. But when startDr Racket
first line should be
#lang scheme
sinceDr Racket
has many language we have explicitly mention which language we are going to use.When we want to pass an argument itself instead of passing the value of the argument then we use quote. It is mostly related to the procedure passing during using lists, pairs and atoms which are not available in C programming Language ( most people start programming using C programming, Hence we get confused) This is code in Scheme programming language which is a dialect of lisp and I guess you can understand this code.
The last line (atom? 'abc) is passing abc as it is to the procedure to check if abc is an atom or not, but when you pass(atom? abc) then it checks for the value of abc and passses the value to it. Since, we haven't provided any value to it
'foo
形式只是一种更快的输入特殊形式的方法,也就是说,“不要评估名称
foo
将其替换为它的值;我真正的意思是名称foo
本身”。我认为 SISC 非常适合探索 TLS 中的练习。
The form
'foo
is simply a faster way to type the special formwhich is to say, "do not evaluate the name
foo
replacing it with its value; I really mean the namefoo
itself".I think SISC is perfectly fine for exploring the exercises in TLS.
您需要了解Scheme的基本评估规则。
First:
上面的列表是一个函数应用程序,因此
atom?
被评估为一个函数。'turkey
是(quote turkey)
的简写符号。计算(quote turkey)
给出符号turkey
。接下来,该函数将应用于符号“turkey”并计算返回值。
第二
我们再次有一个函数应用程序,并且
atom?
被评估为一个函数。这次 turkey 没有被引用,因此是一个变量。评估turkey
给出了与其绑定的值——无论它是什么。然后该函数将应用于变量
turkey
的值。摘要
turkey
是一个变量,它被评估为其值。'turkey
是(quote turkey)
,它被计算为符号turkey
。Scheme 重用 s 表达式并根据 s 表达式构建其程序。这导致了一个问题,有时 turkey 应该是一个变量,有时它应该是符号。这对于初学者来说有点令人困惑。一段时间后,你就会看到它背后的力量。
You need to understand the basic evaluation rules of Scheme.
First:
Above list is a function application, so
atom?
gets evaluated to a function.'turkey
is a short hand notation for(quote turkey)
. Evaluating(quote turkey)
gives the symbolturkey
.So next the function is applied to the symbol
turkey
and a return value is computed.Second
Again we have a function application and
atom?
gets evaluated to a function. This timeturkey
is unquoted and thus a variable. Evaluatingturkey
gives the value that is bound to it - what ever it is.So then the function is applied to the value of the variable
turkey
.Summary
turkey
is a variable, which gets evaluated to its value.'turkey
is(quote turkey)
, which gets evaluated to the symbolturkey
.Scheme reuses s-expressions and builds its programs out of s-expressions. This leads to the problem that sometime
turkey
should be a variable and sometimes it should be the symbol. This is slightly confusing for the beginner. After some time you'll see the power behind it.SISC 很好,但是一个更轻量级的在线方案执行器是 http://codepad.org。它实际上不是 REPL,因为它不是交互式的,但它非常接近。您提交的代码在服务器端执行,而不是使用浏览器小程序。您还可以通过短 URL 共享正在运行的代码。
键盘上的关于页面显示它使用“MzScheme v372 [cgc]”。
我使用键盘进行各种快速代码片段测试(包括测试代码示例以获得答案!)。
对于引用语法,使用如下代码可以看出差异:
显示:
在第一种情况下,
x
被求值并传递给display
,它打印 5。第二种情况,符号x
(与字符串不同)被传递给display
,它会打印名称的符号。SISC is good, but an even more lightweight online Scheme executor is http://codepad.org. It's not actually a REPL in that it's not interactive, but it's pretty close. Code you submit is executed on the server side instead of using a browser applet. And you can share code that you're running by short URL.
The about page on codepad says it uses "MzScheme v372 [cgc]".
I use codepad for all kinds of quick snippet testing (including testing code samples for SO answers!).
For the quote syntax, the difference can be seen using code like this:
This displays:
In the first case,
x
is evaluated and passed todisplay
, which prints 5. In the second case, the symbolx
(which isn't the same thing as a character string) is passed todisplay
, which prints the name of the symbol.(quote ...)
的简写形式,'
将代码转换为数据。stuff
是一个符号,这意味着它可以是变量名或函数名等。'stuff
为您提供符号“stuff”本身。(dostuff "on" those 4 :parameters)
计算时,将运行带有四个参数的函数dostuff
:字符串、变量内容、数字和关键字。'(dostuff "on" those 4 :parameters)
计算时将返回上面的代码,该代码在计算时将依次使用这四个参数运行函数dostuff
。例如:
运行
'''somecode
,它返回''somecode
。运行''somecode
,它返回'somecode
。运行'somecode
,它返回somecode
。运行somecode
,然后...好吧...somecode
将运行。你可以说
'
有点像(eval..)
的反面。(eval (eval (eval '''(print "hello"))))
将打印“Hello”。(eval (eval (eval ''''(print "hello")))
- 注意再一个'
然后eval
- 不会打印任何东西,但它会返回代码(print "hello")
本身!除了 Lispers 倾向于将返回的代码(有时甚至是手写代码)称为“列表”而不是“代码”,因为当你深入挖掘时,原因就会显而易见:祝你好运:)
Shorthand for
(quote ...)
,'
turns code into data.stuff
is a symbol, that means it can be a name of a variable or name of a function, etc..'stuff
gives you the symbol "stuff" itself.(dostuff "on" those 4 :parameters)
when evaluated, would run functiondostuff
with four parameters: string, content of variable those, number and keyword.'(dostuff "on" those 4 :parameters)
when evaluated would return the code above, which, when evaluated, would in turn run functiondostuff
with that four parameters..For example:
Run
'''somecode
, it returns''somecode
. Run''somecode
, it returns'somecode
. Run'somecode
, it returnssomecode
. Runsomecode
, and... well...somecode
will run.You can say that
'
is a bit like the opposite of(eval..)
.(eval (eval (eval '''(print "hello"))))
would print "Hello".(eval (eval (eval ''''(print "hello")))
- notice one more'
theneval
- would not print anything, but it would return the code(print "hello")
itself!!Except that lispers tend to call that returned code (and sometimes even handwritten code) "list" instead of "code", for reasons that will be bleeding obvious as you dig just a bit deeper. Good luck :)
单引号字符是 (quote foo) 的简写方式,其中 quote 是仅返回 foo 而不对其求值的形式。
在Scheme 或任何Lisp 中真正需要记住的一件事是,所有内容都是默认评估的。因此,如果您不想评估,您需要一种方法来解决这个问题。
引用某些内容就可以做到这一点,而单引号只需要更少的输入并导致更少的冗长代码。
The single-quote character is shorthand way of saying (quote foo) where quote is the form to return just foo without evaluating it.
One thing to really remember in Scheme or any Lisp for that matter is that everything is evaluated by default. So, in cases where you don't want to evaluate you need a way to sat this.
Quoting something does just this and the single-quote is just requires less typing and leads to less verbose code.