What does #+#. mean in lisp?

前端 未结 5 2046
不思量自难忘°
不思量自难忘° 2021-02-18 21:26

It is almost impossible to google, hence my understanding is limited to contextual clues from reading through the slime source code: perhaps it is part of the object system in c

5条回答
  •  南旧
    南旧 (楼主)
    2021-02-18 21:31

    That's pretty rare to see.

    #+clim clim:+red+ #-clim mygraphics:+red+
    

    above means that the reader returns either red symbol and it depends whether there is a symbol with the name CLIM is on the list of features *features*. That's a built-in mechanism in Common Lisp.

    #.(cl:if (cl:zerop (cl:random 2)) :high :low)
    

    Above also is a mechanism of the reader. It allows to do computations at read time. Which btw. is a security problem and in Lisp applications it should be disabled - see the variable *read-eval* for controlling this. At read time the reader using READ will return either :HIGH or :LOW, randomly.

    The combination #+#.(FOO) BAR means that the function foo returns a symbol at read time and this symbol then is checked by the reader if there is a symbol with this name on the feature list *features* and if that is the case, then the next item in input is read, otherwise the next item is skipped over.

    Trivial example, IF always returns :CAPI in this example:

    In LispWorks (where CAPI is on the features list):

    CL-USER 41 > (read-from-string "#+#.(cl:if cl:t :capi :clim) a b")
    A
    31
    

    In SBCL

    * (read-from-string "#+#.(cl:if cl:t :capi :clim) a b")
    
    B
    32
    

提交回复
热议问题