What does #+#. mean in lisp?

久未见 提交于 2019-12-21 06:56:52

问题


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 common lisp? Something like 'self'?

snippet:

(cond #+#.(swank-backend::sbcl-with-new-stepper-p)

Perhaps this will make it more googleable : pound plus pound // hash plus hash symbol // octothorp plus octothorp


回答1:


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



回答2:


It's actually Sharpsign Plus followed by Sharpsign Dot.




回答3:


They're Common Lisp reader macro characters:

a textual notation introduced by dispatch on one or two characters that defines special-purpose syntax for use by the Lisp reader, and that is implemented by a reader macro function.

Reader macros should not be confused with regular macros - they have nothing to do with each other.

The set-dispatch-macro-character function can be used to extend the Common Lisp syntax with custom reader macros.




回答4:


Also, #-foo(code to execute) will execute the code only if foo is not in *features*.

Information about #+, #., etc. is difficult to find even in the Common Lisp HyperSpec, but relevant HyperSpec pages can be found by Googling "Sharpsign minus", etc. (Thanks Austin.)




回答5:


2.4.8.17 Sharpsign Plus http://www.lispworks.com/documentation/HyperSpec/Body/02_dhq.htm #+test expression Readtime macro, if test is true read expression, otherwise read it as white space.

2.4.8.6 Sharpsign Dot http://www.lispworks.com/documentation/HyperSpec/Body/02_dhf.htm The #. foo syntax performs a read-time evaluation of foo.

http://www.lispworks.com/documentation/HyperSpec/Body/02_dh.htm All the #X operators (defined by the Lisp HyperSpec)

Why does the Lisp HyperSpec have to be so hard to read? Probably because it's done by committee. I say it's one of the reasons Lisp is not popular. Also, Lisp is too academic, and the barriers-to-entry are too high (the learning curve and support community are not so great... there aren't 10 thousand little programs out there to easily get you started (in some arcane implementation you are using)).



来源:https://stackoverflow.com/questions/5785755/what-does-mean-in-lisp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!