什么是' (撇号)在 Lisp/Scheme 中?

发布于 2024-08-07 07:47:17 字数 503 浏览 6 评论 0原文

我正在自学计划的第一天第一个小时。不用说,我什么也不明白。所以我正在阅读《小阴谋家》并使用这个东西:

http://sisc-scheme.org/sisc-online.php< /p>

作为解释器。

例如,我需要使用 '

(atom? 'turkey)

以避免“未定义的变量”错误。根据这本书,' 是 Common Lisp 的东西。

我有两个问题:

  1. 我上面提到的翻译是一个好的翻译吗?你能推荐另一个吗?我需要一个能够与小阴谋家配合使用的翻译器。

  2. 什么是'

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:

http://sisc-scheme.org/sisc-online.php

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:

  1. Is the interpreter I mentioned above a good one? Can you recommend another? I need one that will go well with The Little Schemer.

  2. What is '?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

欲拥i 2024-08-14 07:47:18
  1. 我建议你转移到一个更好的环境,比如PLT Scheme,它有一个IDE,调试器和许多库。当您继续前进并开始编写更大的程序时,您将需要它们。

  2. 单引号字符是“quote”表达式的语法糖,因此 'turkey 与 (quote turkey) 相同。基本上,“引用”的作用是关闭方案评估器。换句话说,“quote”逐字返回表达式。如果没有“quote”,那么Scheme会尝试在当前环境下评估“turkey”。这不是 Common Lisp 的东西,而是 Lisp 的东西。 Common Lisp 和Scheme 是Lisp 的两种方言。所有 Lisp 教程/书籍中都解释了“quote”的用法。另请参阅此问题的答案。

  1. 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.

  2. 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.

疑心病 2024-08-14 07:47:18

如果您正在寻找 scheme 的最佳 IDE,那么请选择 Racket。但是当启动Racket博士
第一行应该是#lang schema,因为Racket有很多语言,我们已经明确提到了我们将使用哪种语言。

当我们想要传递参数本身而不是传递参数的值时,我们可以使用 quote。它主要与使用列表、对和原子期间的过程传递有关,这些在 C 编程语言中不可用(大多数人开始使用 C 编程进行编程,因此我们会感到困惑)这是Scheme编程语言中的代码,它是 Lisp 的一种方言我想你能理解这段代码。

(define atom?              ; defining a procedure atom?
(lambda (x)              ; which as one argument x
(and (not (null? x)) (not(pair? x) )))) ; checks if the argument is atom or not
(atom? '(a b c)) ; since it is a list it is false #f

最后一行 (atom? 'abc) 传递 abc ,因为它是检查 abc 是否是原子的过程,但是当您传递 (atom? abc) 时,它会检查 abc 的值并将该值传递给它。因为,我们没有为其提供任何价值

If you looking for a best IDE for scheme then go for Dr Racket. But when start Dr Racket
first line should be #lang scheme since Dr 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.

(define atom?              ; defining a procedure atom?
(lambda (x)              ; which as one argument x
(and (not (null? x)) (not(pair? x) )))) ; checks if the argument is atom or not
(atom? '(a b c)) ; since it is a list it is false #f

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

一影成城 2024-08-14 07:47:17

'foo 形式只是一种更快的输入特殊形式的方法

(quote foo)

,也就是说,“不要评估名称 foo 将其替换为它的值;我真正的意思是名称 foo 本身”。

我认为 SISC 非常适合探索 TLS 中的练习。

The form 'foo is simply a faster way to type the special form

(quote foo)

which is to say, "do not evaluate the name foo replacing it with its value; I really mean the name foo itself".

I think SISC is perfectly fine for exploring the exercises in TLS.

八巷 2024-08-14 07:47:17

您需要了解Scheme的基本评估规则。

First

(atom? 'turkey)

上面的列表是一个函数应用程序,因此 atom? 被评估为一个函数。
'turkey(quote turkey) 的简写符号。计算(quote turkey) 给出符号turkey

接下来,该函数将应用于符号“turkey”并计算返回值。

第二

(atom? 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:

(atom? 'turkey)

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 symbol turkey.

So next the function is applied to the symbol turkey and a return value is computed.

Second

(atom? turkey)

Again we have a function application and atom? gets evaluated to a function. This time turkey is unquoted and thus a variable. Evaluating turkey 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 symbol turkey.

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.

若能看破又如何 2024-08-14 07:47:17

SISC 很好,但是一个更轻量级的在线方案执行器是 http://codepad.org。它实际上不是 REPL,因为它不是交互式的,但它非常接近。您提交的代码在服务器端执行,而不是使用浏览器小程序。您还可以通过短 URL 共享正在运行的代码。

键盘上的关于页面显示它使用“MzScheme v372 [cgc]”。

我使用键盘进行各种快速代码片段测试(包括测试代码示例以获得答案!)。

对于引用语法,使用如下代码可以看出差异:

(let ((x 5))
  (display x) (newline)
  (display 'x) (newline))

显示:

5
x

在第一种情况下,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:

(let ((x 5))
  (display x) (newline)
  (display 'x) (newline))

This displays:

5
x

In the first case, x is evaluated and passed to display, which prints 5. In the second case, the symbol x (which isn't the same thing as a character string) is passed to display, which prints the name of the symbol.

送君千里 2024-08-14 07:47:17

(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 function dostuff 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 function dostuff with that four parameters..

For example:
Run '''somecode, it returns ''somecode. Run ''somecode, it returns 'somecode. Run 'somecode, it returns somecode. Run somecode, 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 ' then eval - 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 :)

清晨说晚安 2024-08-14 07:47:17

单引号字符是 (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.

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