What is ' (apostrophe) in Lisp / Scheme?

自闭症网瘾萝莉.ら 提交于 2019-12-02 19:55:47

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 and replace it with its value; I really mean the name foo".

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

You need to understand the basic evaluation rules of Scheme.

First:

(atom? 'turkey)

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

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.

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 :)

Vijay Mathew
  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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!