What is ' (apostrophe) in Lisp / Scheme?

后端 未结 7 651
情深已故
情深已故 2021-02-01 12:16

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:

相关标签:
7条回答
  • 2021-02-01 12:32

    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.

    0 讨论(0)
  • 2021-02-01 12:38

    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.

    0 讨论(0)
  • 2021-02-01 12:39

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

    0 讨论(0)
  • 2021-02-01 12:51

    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.

    0 讨论(0)
  • 2021-02-01 12:52

    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

    0 讨论(0)
  • 2021-02-01 12:58
    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.

    0 讨论(0)
提交回复
热议问题