在阅读了大量有关Lisp eval- when
运算符的文件后, 我仍然无法理解它的用途, 我知道这个运算符能控制我表达式的评价时间, 但我无法找到任何可能适用的例子?
Best Regards, utxeee.
在阅读了大量有关Lisp eval- when
运算符的文件后, 我仍然无法理解它的用途, 我知道这个运算符能控制我表达式的评价时间, 但我无法找到任何可能适用的例子?
Best Regards, utxeee.
<% 1\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ <\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
例如, Lisp 文件的编译。 Lisp 编译器处理顶层表格。 这些可以是任意的 Lisp 格式、 DeFUN 格式、 DeFMACROS 格式、 DeFMACROS 格式、 DeFCLASS 格式、 函数调用...
整个文件编译器如何工作的故事太复杂, 无法在此解释, 但有几件事:
文件编纂器为 (DEFUN foo ())
form ()) < /code> 生成代码。但它不执行 defun 窗体。因此,在汇编过程中,已知有一个函数 FOO
,但在汇编过程中找不到 {FOO___的代码。编纂器为汇编的文件生成代码,但不会将其保存在内存中。您无法在汇编时调用此函数 。
对于宏,它的工作略为不同: (DEFMACRO BAZ...)
。文件汇编器不仅将汇编宏并注意它存在,而且还会在汇编时提供宏。它被装入汇编器环境 。
因此,想象文件中的表格序列 :
(defmacro baz ...)
(defun foo () (baz ...))
这是因为文件编译者知道宏 BA/code>, 当它编辑
FOO
的代码时, 它可以扩展宏表单 。
现在让我们看看下面的例子:
(defun bar (form) ...)
(defmacro baz (form) (bar form))
(defun foo () (baz ...))
上面没有工作。 现在, 宏 < code>BA_ {/ code> 使用函数 < code>BAR code> 来调用它。 当编译者试图编译函数 < code> FOO code> 时, 它无法扩展 < code>BA_ {/ code> 宏, 因为 < code> BAR code> 无法被调用, 因为 < code> BAR code> 的代码没有被装入编译时间环境 。
对此有两种解决办法:
BAR
earlier using a separate file.EVAL-WHEN
示例:
(eval-when (:compile-toplevel :execute :load-toplevel)
(defun bar (form) ...)
)
(defmacro baz (form) (bar form))
(defun foo () (baz ...))
现在 EVAL-WHEN
指示文件编纂者在汇编过程中实际运行 DEFUN 格式。 效果是: 文件编纂者现在在 < em> complile time em> 上知道 < code> 的定义 < code> < bAR 。 因此,在以后, 当文件编纂者在宏扩展 < code> BaBA_ /code的使用时需要调用 < code> < bAR 时, 就可以使用 。
人们只能使用 :compile-toplevel
, 当文件汇编后不需要此函数时。 如果稍后使用, 那么我们需要确保它被装入 。
EVAL-WHEN
允许指定是否应该运行特定的代码件
EVAL-WHEN
在用户代码中没有经常使用。 如果您使用它, 那么您应该问问自己是否真的需要它 。
Long story: I am doing a project for my functional programing class, and I thought of writing an AI controller in Lisp, for the Mario AI competition. I was looking over frameworks/libraries/ways of ...
I use emacs via Putty and since Putty doesn t send certain key combinations to the remote console I generally need to re-bind them to other key combinations. After installing the amazing Zen-Coding ...
I develop in Lisp and in Scheme, but I was reading about Clojure and then I want to know, in which cases is better to use it than using Lisp or Scheme? Thanks
If I have a list of 0 s, how would I modify, for example, the 16th 0 in the list?
I was trying to write my own put-pixel on (Gdk) pixbuf in Lisp. When I finally realized how I can operate on C pointers in CL, new obstacle came along - (gdk:pixbuf-get-pixels pb) returns me negative ...
I have created some of my own user packages and have run into a name clash. In Java, the naming convention is to use your domain name in the package name: e.g. import com.example.somepackage;. Are ...
We have built a webservice for a client that uses AutoCAD. They have a macro that runs in AutoCAD that builds a SOAP request. But they have not figured out how to actually send() the soap request to ...
In Chapter 9 of the Little Schemer, the Author presents the following two functions (define Q (lambda (str n) (cond ((zero? (remainder (first$ str ) n)) (Q (second$ str ) n)) ...