The eval-when in the preceding expression is not at top level, so only the :execute keyword is considered. At compile time, this has no effect. At load time (if the let is at top level), or at execution time (if the let is embedded in some other form which does not execute until later), this sets (symbol-function 'foo1) to a function that returns 1.
If the preceding expression occurs at the top level of a file to be compiled, it has both a compile time and a load-time effect of setting (symbol-function 'foo2) to a function that returns 2.
If the preceding expression occurs at the top level of a file to be compiled, it has both a compile time and a load-time effect of setting the function cell of foo3 to a function that returns 3.
If the preceding form occurs at the top level of a file to be compiled, foo5 is printed at compile time. If this form occurs in a non-top-level position, nothing is printed at compile time. Regardless of context, nothing is ever printed at load time or execution time.
If the preceding form occurs at the top level of a file to be compiled, foo6 is printed at compile time. If this form occurs in a non-top-level position, nothing is printed at compile time. Regardless of context, nothing is ever printed at load time or execution time.
发布评论
评论(9)
我是指 load时 会打印两个 a, compile-file当然是一个.
本帖最后由 win_hate 于 2010-07-17 12:57 编辑
这个是代码,我把它叫 1.lisp
复制代码我这里无论用什么参数,总是一个 a
复制代码
复制代码
复制代码
楼上,要用 :verbose :print 才看到两个 a
否则只看到返回值.
本帖最后由 win_hate 于 2010-07-17 12:03 编辑
两个 a,其中一个是 print 表达式的值,一般这个值在交互时才会显示。我的 clisp 是 2.44.1 的,load, compile-file 时都显示一个 a. 不过这个只涉及 clisp 的设计,跟语言关系不大。
http://www.cs.cmu.edu/Groups/AI/ ... iss147-writeup.html
好没耐心打字哦.
eval-when总会返回最后一个form的执行结果.
a.lisp
(defmacro foo (x) `(car ,x))
(print (foo '(a b c)))
你对比下(compile-file 'a.lisp) 与 你的(compile-file 'mm.lisp)
top-level: 当代码在顶层时才...
假如:
(defun test ()
(eval-when ....)
这样,compile后你是调用不到foo的.因为 test系顶层.
本帖最后由 xdshting 于 2010-07-10 10:21 编辑
1,这是cltl中的一个例子
(eval-when (eval compile load)
(defmacro foo (x) `(car ,x))
(print (foo '(a b c))))
你能解释一下,这个form的运行过程吗,比如(compile-file。。。)时做什么,(load ***.fas)时做什么
2,下面是我的结果,clisp2.48,特别是对于(load "mm.fas")输出了两个A,是因为print的关系,但是为什么complie的时候只输出一个A,这和compile有关系?很不解,(load "mm.lisp")也是输出两个A
3,
(defmacro foo (x) `(car ,x))
(eval-when (:execute :compile-toplevel :load-toplevel)
(print (foo '(a b c))))
此定义与上面的定义功能上有什么区别?
谢谢
本帖最后由 miniqq 于 2010-06-26 19:25 编辑
注释不都很明显嘛,楼主别钻牛角尖哦.
看看eval-when的用法,
(eval-when (way)
*body*)
way包括: :execute :load-toplevel :compile-toplevel
如果eval-when这个form在toplevel就执行load或者compile.类似:
(if (load-toplevel) load) 对*body*进行load
(if (compile-toplevel) compile) 对*body*进行 compile
(else (execute) eval) 直接执行*body*
这三种情况.
我觉得你好像是不理解 eval, load, compile都做些什么!
不是你不理解eval-when.
忘记了说eval-when的(way)列表可以包括:compile-toplevel, :load-toplevel, :execute, compile, load, or eval. 几个符号
不知道对你有没有帮助.
Here are some additional examples.
(let ((x 1))
(eval-when (:execute :load-toplevel :compile-toplevel)
(setf (symbol-function 'foo1) #'(lambda () x))))
The eval-when in the preceding expression is not at top level, so only the :execute keyword is considered. At compile time, this has no effect. At load time (if the let is at top level), or at execution time (if the let is embedded in some other form which does not execute until later), this sets (symbol-function 'foo1) to a function that returns 1.
(eval-when (:execute :load-toplevel :compile-toplevel)
(let ((x 2))
(eval-when (:execute :load-toplevel :compile-toplevel)
(setf (symbol-function 'foo2) #'(lambda () x)))))
If the preceding expression occurs at the top level of a file to be compiled, it has both a compile time and a load-time effect of setting (symbol-function 'foo2) to a function that returns 2.
(eval-when (:execute :load-toplevel :compile-toplevel)
(setf (symbol-function 'foo3) #'(lambda () 3)))
If the preceding expression occurs at the top level of a file to be compiled, it has both a compile time and a load-time effect of setting the function cell of foo3 to a function that returns 3.
(eval-when (:compile-toplevel)
(eval-when (:compile-toplevel)
(print 'foo4)))
The preceding expression always does nothing; it simply returns nil.
(eval-when (:compile-toplevel)
(eval-when (:execute)
(print 'foo5)))
If the preceding form occurs at the top level of a file to be compiled, foo5 is printed at compile time. If this form occurs in a non-top-level position, nothing is printed at compile time. Regardless of context, nothing is ever printed at load time or execution time.
(eval-when (:execute :load-toplevel)
(eval-when (:compile-toplevel)
(print 'foo6)))
If the preceding form occurs at the top level of a file to be compiled, foo6 is printed at compile time. If this form occurs in a non-top-level position, nothing is printed at compile time. Regardless of context, nothing is ever printed at load time or execution time.
这是几个例子,特别是eval-when的嵌套,看得有点晕,请您解释一下
谢谢
本帖最后由 miniqq 于 2010-07-17 09:34 编辑
eval-when一般用来处理源文件,这个事关 side-effect的学问.
常用有三种方式:load-toplevel :compile-toplevel :execute
load 文件时里面所有的form都会被顺序执行
compile时相当于 load fasl 文件(机器码) (emacs里有 eval-when-compile)
execute / eval 直接一条条执行
如下:
(eval-when (compile) (some forms...))
(eval-when (load) (some forms...))
(eval-when (eval) (some forms...)