What does #+#. mean in lisp?
Asked Answered
B

5

21

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

Bergren answered 26/4, 2011 at 3:59 Comment(5)
Nice Googleability addition. People should always do that.Monocotyledon
Wow, never knew it was called an octothorp.Secondclass
'octothorp' or 'octothorpe'? :-) Google seems to suggest the latter is more widely used.Veronique
Check out SymbolHound, and same goes for every programmer out there. It's very helpful in these cases.Navada
Don't forget sharp plus sharp dot. :)Septal
M
19

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
Merril answered 26/4, 2011 at 6:27 Comment(0)
T
16

It's actually Sharpsign Plus followed by Sharpsign Dot.

Thuthucydides answered 26/4, 2011 at 4:17 Comment(0)
L
4

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.

Lintwhite answered 26/4, 2011 at 6:52 Comment(0)
C
0

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.)

Caxton answered 1/1, 2013 at 6:57 Comment(0)
C
0

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)).

Contraception answered 25/4, 2013 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.