What is ' (apostrophe) in Lisp / Scheme?

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

提交回复
热议问题