免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 9825 | 回复: 10
打印 上一主题 下一主题

lisp里面,eval-when 到底怎么用? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-06-25 21:34 |只看该作者 |倒序浏览
谢谢,看了一些英文资料,还是搞不太清楚

论坛徽章:
2
白羊座
日期:2013-10-29 13:29:222015亚冠之全北现代
日期:2015-10-25 08:13:02
2 [报告]
发表于 2010-06-26 16:04 |只看该作者
本帖最后由 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...)

论坛徽章:
0
3 [报告]
发表于 2010-06-26 16:33 |只看该作者
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的嵌套,看得有点晕,请您解释一下
谢谢

论坛徽章:
2
白羊座
日期:2013-10-29 13:29:222015亚冠之全北现代
日期:2015-10-25 08:13:02
4 [报告]
发表于 2010-06-26 19:10 |只看该作者
本帖最后由 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.  几个符号

不知道对你有没有帮助.

论坛徽章:
0
5 [报告]
发表于 2010-07-09 21:03 |只看该作者
本帖最后由 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))))   
此定义与上面的定义功能上有什么区别?
谢谢

未命名.GIF (11.33 KB, 下载次数: 65)

未命名.GIF

论坛徽章:
2
白羊座
日期:2013-10-29 13:29:222015亚冠之全北现代
日期:2015-10-25 08:13:02
6 [报告]
发表于 2010-07-17 09:33 |只看该作者
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系顶层.

论坛徽章:
0
7 [报告]
发表于 2010-07-17 12:00 |只看该作者
本帖最后由 win_hate 于 2010-07-17 12:03 编辑

两个 a,其中一个是 print 表达式的值,一般这个值在交互时才会显示。我的 clisp 是 2.44.1 的,load, compile-file 时都显示一个 a. 不过这个只涉及 clisp 的设计,跟语言关系不大。

论坛徽章:
2
白羊座
日期:2013-10-29 13:29:222015亚冠之全北现代
日期:2015-10-25 08:13:02
8 [报告]
发表于 2010-07-17 12:25 |只看该作者
楼上,要用 :verbose :print 才看到两个 a
否则只看到返回值.

论坛徽章:
0
9 [报告]
发表于 2010-07-17 12:53 |只看该作者
本帖最后由 win_hate 于 2010-07-17 12:57 编辑

这个是代码,我把它叫 1.lisp

  1. (eval-when (eval compile load)
  2.   (defmacro foo (x) `(car ,x))
  3.   (print (foo '(a b c))))
复制代码
我这里无论用什么参数,总是一个 a

  1. [1]> (compile-file "1.lisp")
  2. ;; Compiling file /home/zxl/1.lisp ...
  3. A
  4. ;; Wrote file /home/zxl/1.fas
  5. 0 errors, 0 warnings
  6. #P"/home/zxl/1.fas" ;
  7. NIL ;
  8. NIL
复制代码

  1. [2]> (compile-file "1.lisp" :verbose t :print t)
  2. ;; Compiling file /home/zxl/1.lisp ...
  3. ; (EVAL-WHEN (EVAL COMPILE LOAD) (DEFMACRO FOO # ...) ...)
  4. A
  5. ;; Wrote file /home/zxl/1.fas
  6. 0 errors, 0 warnings
  7. #P"/home/zxl/1.fas" ;
  8. NIL ;
  9. NIL
复制代码

  1. [3]> (compile-file "1.lisp" :verbose nil :print nil)

  2. A
  3. #P"/home/zxl/1.fas" ;
  4. NIL ;
  5. NIL
复制代码

论坛徽章:
2
白羊座
日期:2013-10-29 13:29:222015亚冠之全北现代
日期:2015-10-25 08:13:02
10 [报告]
发表于 2010-07-17 13:51 |只看该作者
两个 a,其中一个是 print 表达式的值,一般这个值在交互时才会显示。我的 clisp 是 2.44.1 的,load, comp ...
win_hate 发表于 2010-07-17 12:00



    我是指 load时 会打印两个 a, compile-file当然是一个.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP