What's the benefit of using Sharpsign Dot?
Asked Answered
M

2

6

I had used cl-ppcre package recently and I am very curious about how they optimize this package because I want to learn optimizing common lisp. I notice they use Sharpsign Dot a lot in declare expression, like here. The optimized setting is here.

Why do they write like this? Is there any benefit? Or some rule has them to do?

Update: I run

(defvar *b* '(optimize speed))
(pprint (macroexpand-1 '(declaim *b*)))
(pprint (macroexpand-1 '(declaim #.*b*))) ;; => this one is right

So, #. will eval value even before macro call? Like it eval the value first and replaces it in the macro argument.

Marthmartha answered 22/11, 2019 at 23:17 Comment(0)
B
7

Sharpsign Dot will evaluate the next expression at read time. Thus it will act as the reader got fed the result and the other levels of CL don't know this. Macros that rely on literals cannot be made dynamic because of the evaluation rules so s read time macro will circumvent that and make it as the dynamic expression was literal for the other levels than the reader.

The benefit is an extra level of meta programming.

Bavardage answered 22/11, 2019 at 23:39 Comment(1)
To connect this answer to the original question: declaim is a macro that takes as parameter "a declaration specifier; not evaluated."Axenic
T
5

See the difference:

CL-USER 7 > (defvar *answer* 42)
*ANSWER*

CL-USER 8 > '(*answer* #.*answer*)
(*ANSWER* 42)

#. allows expressions to be evaluated at read-time. The result will be returned from the reader - instead of the original expression.

CL-USER 9 > '(*answer* (* #.*answer* pi) #.(* pi *answer*))
(*ANSWER* (* 42 PI) 131.94689145077132D0)

Note that the value of *answer* needs to be known at read-time to make this work.

Teetotum answered 23/11, 2019 at 16:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.