Why is #' (sharp-quote) notation unnecessary in CLISP?

后端 未结 3 778
执念已碎
执念已碎 2021-01-18 06:47

I\'m learning Lisp from the book \'Practical Common Lisp\'. At one point, I\'m supposed to enter the following bit of code:

[1] (remove-if-not #\'evenp \'(1          


        
相关标签:
3条回答
  • 2021-01-18 07:20

    Sharp-quote and quote do not have the same behaviour in the general case:

    (defun test () 'red)
    
    (flet ((test () 'green))
      (list (funcall 'test)
            (funcall #'test))) => (red green)
    

    Calling a quoted symbol will use the function value of the quoted symbol (ie, the result of symbol-function). Calling a sharp-quoted symbol will use the value established by the lexical binding, if any, of the symbol. In the admittedly common case that there is no lexical binding the behaviour will be the same. That's what you are seeing.

    You should get into the habit of using sharp-quote. Ignoring function bindings is probably not what you want, and may be confusing to anybody trying to understand your code.

    0 讨论(0)
  • 2021-01-18 07:24

    As you have noticed (or read) funcall et. al. are will make an effort to convert the function argument you provide into something approprate. So as you have noticed they will take a symbol and then fetch the symbol-function of that symbol; if that works out they will then invoke that.

    Recall that #'X is converted at readtime into (symbol-function x) and 'x into (quote x). It's good practice to have the symbol-function work done at compile time.

    But why? Well two trival reasons it is slightly faster and it signals that you don't intend to redefine F's symbol-function after compile time. Another reason is that in a recent Pew Research study 98.3% of Lisp developers prefer it, and 62.3% will shun those that don't do this.

    But there's more.

    '(lambda (..) ...) is quite different v.s. #'(lambda (..) ...). The first is very likely to end up using eval, i.e. it will be slow. The first runs in a different scope v.s. the second one, i.e. only the second one can see the lexical scope it appears in.

    0 讨论(0)
  • 2021-01-18 07:41

    This is not CLISP specific, it works in every Common Lisp implementation (I use Clozure Common Lisp here).

    What happens is that if you give a symbol as a function designator then the implementation will look up the symbol-function (assuming the symbol is available in the global environment) for you:

    ? #'evenp
    #<Compiled-function EVENP #x3000000F2D4F>
    ? (symbol-function 'evenp)
    #<Compiled-function EVENP #x3000000F2D4F>
    

    In general you can use either, but there's an interesting effect if you rebind the called function later. If you specify the function (#' or (function)) then the calls will still call the old function because the lookup has been done at compile time; if you use the symbol then you will call the new function because the lookup is re-done at runtime. Note that this may be implementation-specific.

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