处理槽和字符串列表中的值
我想在 common lisp 中做一个宏,它应该接受一个由槽和字符串组成的列表的参数。这是原型:
(defclass time-info ()
((name :initarg name)
(calls :initarg calls)
(second :initarg second)
(consing :initarg consing)
(gc-run-time :initarg gc-run-time)))
(defun print-table (output arg-list time-info-list) ())
这个想法是根据定义其结构的 arg-list 打印一个表。下面是调用该函数的示例:
(print-table *trace-output*
'("|" name "||" calls "|" second "\")
my-time-info-list)
这会在跟踪输出上以 ascII 格式打印一个表。问题是我不知道如何明确获取列表的元素以在宏的不同部分中使用它们。
我还不知道如何做到这一点,但我确信它可以做到。也许你可以帮助我:)
I want to do a macro in common lisp which is supposed to take in one of its arguments a list made of slots and strings. Here is the prototype :
(defclass time-info ()
((name :initarg name)
(calls :initarg calls)
(second :initarg second)
(consing :initarg consing)
(gc-run-time :initarg gc-run-time)))
(defun print-table (output arg-list time-info-list) ())
The idea is to print a table based on the arg-list which defines its structure. Here is an example of a call to the function:
(print-table *trace-output*
'("|" name "||" calls "|" second "\")
my-time-info-list)
This print a table in ascII on the trace output. The problem, is that I don't know how to explicitely get the elements of the list to use them in the different parts of my macro.
I have no idea how to do this yet, but I'm sure it can be done. Maybe you can help me :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将基于
格式
。这个想法是构建一个格式字符串来自您的
arg-list
。我为此定义了一个辅助函数:
请注意,
~
必须在format
字符串中加倍才能转义它们。然后,打印宏本身只会生成
format
的mapcar
:然后您可以这样调用它:
请注意代码中的以下错误:
您需要转义字符串中的
\
。Second
已经是从common-lisp
导出的函数名称包裹。您不应该使用通用函数来破坏它。
I would base this on
format
. The idea is to build a format stringfrom your
arg-list
.I define a helper function for that:
Note that
~
must be doubled informat
strings in order to escape them.The printing macro itself then just produces a
mapcar
offormat
:You can then call it like this:
Please note the following errors in your code:
You need to escape
\
in strings.Second
is already a function name exported from thecommon-lisp
package. You should not clobber that with a generic function.
您需要更准确地表达您的要求。宏和函数是不同的东西。数组和列表也不同。
我们需要迭代
TIME-INFO-LIST
。这就是第一个DOLIST
。该表有一条线路的描述。描述中的每一项都是插槽名称或字符串。所以我们迭代描述。这是第二个
DOLIST
。刚刚打印了一个字符串。符号是一个槽名称,我们可以从当前的 time-info 实例中检索槽值。测试:
You need to be more precise with your requirements. Macros and Functions are different things. Arrays and Lists are also different.
We need to iterate over the
TIME-INFO-LIST
. So that's the firstDOLIST
.The table has a description for a line. Each item in the description is either a slot-name or a string. So we iterate over the description. That's the second
DOLIST
. A string is just printed. A symbol is a slot-name, where we retrieve the slot-value from the currenttime-info
instance.Test:
首先,如果您使用宏,您可能不希望在那里使用引号(但是,如果您使用函数,则确实需要它)。其次,您希望分隔符和值之间有填充吗?第三,使用函数可能比宏更好。
您似乎还可以互换使用“数组”和“列表”。它们在 Common Lisp 中是完全不同的东西。有些操作适用于通用序列,但通常您会使用一种方法来迭代列表,另一种方法来迭代数组。
First, you probably don't want the quote there, if you're using a macro (you do want it there if you're using a function, however). Second, do you want any padding between your separators and your values? Third, you're probably better off with a function, rather than a macro.
You also seem to be using "array" and "list" interchangeably. They're quite different things in Common Lisp. There are operations that work on generic sequences, but typically you would use one way of iterating over a list and another to iterate over an array.