Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the benefit of using Sharpsign Dot?

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.

like image 410
ccQpein Avatar asked Nov 22 '19 23:11

ccQpein


2 Answers

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.

like image 76
Sylwester Avatar answered Sep 28 '22 06:09

Sylwester


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.

like image 28
Rainer Joswig Avatar answered Sep 28 '22 08:09

Rainer Joswig