我最近使用过cl-ppcre软件包,我对它们如何优化此软件包非常好奇,因为我想学习优化常见的Lisp。我注意到他们在声明表达式中大量使用Sharpsign Dot,例如here。优化设置为here。
他们为什么这样写?有什么好处吗?还是他们要遵守某些规则?
更新: 我跑
(defvar *b* '(optimize speed))
(pprint (macroexpand-1 '(declaim *b*)))
(pprint (macroexpand-1 '(declaim #.*b*))) ;; => this one is right
因此,#.
会在宏调用之前评估值吗?像它一样先评估该值,然后将其替换为宏参数。
答案 0 :(得分:8)
Sharpsign Dot将在读取时评估下一个表达式。因此,它将作为读者获取结果的手段,而CL的其他级别则不知道这一点。依赖于评估规则,不能使依赖于文字的宏变为动态的,因此s的读取时间宏将避免这种情况,并使其成为动态表达式,而对于其他级别的读者而言,则是动态的。
好处是元编程的额外级别。
答案 1 :(得分:4)
查看区别:
int[] aa= new int[]{123456789};
if(aa.length==10)
{
for (int i=0;i<=aa.length;i++)
{
system.out.println(aa[i]);
}
}
if(aa.length>0)
{
for(int i=0;i<=10;i++)
{
system.out.println(aa[i]);
}
}
if(aa.length<10)
{
for(int i=0;i<=10;i++)
{
system.out.println(aa[i]);
}
}
CL-USER 7 > (defvar *answer* 42)
*ANSWER*
CL-USER 8 > '(*answer* #.*answer*)
(*ANSWER* 42)
允许在读取时对表达式求值。结果将从阅读器返回-而不是原始表达式。
#.
请注意,CL-USER 9 > '(*answer* (* #.*answer* pi) #.(* pi *answer*))
(*ANSWER* (* 42 PI) 131.94689145077132D0)
的值需要在读取时知道才能完成。