What is ' (apostrophe) in Lisp / Scheme?

后端 未结 7 661
情深已故
情深已故 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: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 :)

提交回复
热议问题