如何在Common Lisp中的宏外部拼接到列表? [英] How do I splice into a list outside of a macro in Common Lisp?

查看:78
本文介绍了如何在Common Lisp中的宏外部拼接到列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个函数foo:

Say I have a function foo:

(defun foo(xy& rest args)
...)

然后我想用功能栏将其包装:

And I later want to wrap it with a function bar:

(defun bar(x& rest args)
(foo x 100 args))

假设条形为然后这样称呼:(bar 50 1 2 3)

Assume bar was then called like this: (bar 50 1 2 3)

通过此设置,args是包含尾随参数的bar主体,因此当我将其传递给foo时,而不是得到(foo 50 100 1 2 3)的值,我当然得到了(foo 50100'(1 2 3))。如果这些是宏,我会在bar主体中使用``(foo,x 100,@ args)`将args拼接到函数调用中。 ,@@仅在反引号引起的列表中起作用。

With this setup, args is a list within the body of bar that holds the trailing parameters, so when I pass it to foo, instead of getting the equivalent of (foo 50 100 1 2 3) I of course get (foo 50 100 '(1 2 3)). If these were macros, I would use ``(foo ,x 100 ,@args)` within the body of bar to splice args into the function call. ,@ only works inside a backtick-quoted list, however.

我如何在常规函数中进行这种拼接?

How can I do this same sort of splicing within a regular function?

推荐答案

APPLY将调用其第一个参数及其后续参数,而最后一个参数必须是列表。因此:

APPLY will call its first argument with its subsequent arguments, and the last argument must be a list. So:

(apply #'foo x 100 args)

这篇关于如何在Common Lisp中的宏外部拼接到列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆