Lisp:检查函数确定其所需的参数
在 Python 中,我可以这样做:
>>> def foo(x,y,z=1):
return x+y*z
>>> foo.func_code.co_varnames
('x', 'y', 'z')
>>> foo.func_defaults
(1,)
并从中知道我必须有多少个参数才能调用 foo()。我怎样才能在 Common Lisp 中做到这一点?
In Python, I can do this:
>>> def foo(x,y,z=1):
return x+y*z
>>> foo.func_code.co_varnames
('x', 'y', 'z')
>>> foo.func_defaults
(1,)
And from it, know how many parameters I must have in order to call foo(). How can I do this in Common Lisp?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
大多数实现都提供了执行此操作的方法,但没有一个是标准化的。如果你确实需要它,Swank(SLIME 的 Common Lisp 部分)有一个名为 < code>swank-backend:arglist 据我所知,它可以满足您的需求:
不过,我不确定您是否可以依赖它在将来仍然可用。
Most implementations provide a way of doing this, but none is standardized. If you absolutely need it, Swank (the Common Lisp part of SLIME) has a function called
swank-backend:arglist
that, as far as I can see, does what you want:I'm not sure you can rely on it remaining available in the future, though.
通常大多数 Lisp 在某个包中都有一个名为 ARGLIST 的函数。 LispWorks 将其称为“FUNCTION-LAMBDA-LIST”。
出于 LispWorks 中的信息目的,如果将光标放在函数符号上,则 control-shift-a 将显示 arglist。在 LispWorks 中还有一个可以加载的“arglist-on-space”功能。输入符号和空格后,IDE 将显示参数列表。
还有 CL:DESCRIBE 函数。它描述了各种对象。在大多数 CL 实现中,它还应该显示函数的参数列表。
以下示例适用于 Clozure Common Lisp:
Usually most Lisps have a function called ARGLIST in some package. LispWorks calls it FUNCTION-LAMBDA-LIST.
For information purposes in LispWorks, if one has the cursor on a function symbol, then control-shift-a displays the arglist. In LispWorks there is also an 'arglist-on-space' functionality that can be loaded. After typing a symbol and a space, the IDE displays the arglist.
There is also the CL:DESCRIBE function. It describes various objects. In most CL implementations it also should display the arglist of a function.
The following example is for Clozure Common Lisp:
如果你想在编辑时知道这一点,SLIME+emacs 会为你解决这个问题。
例如,在 emacs lisp-mode + slime 中,输入
(format
将在底部的迷你缓冲区中显示 format 的参数。
If you want to know this just when editing, SLIME+emacs will take care of that for you.
e.g. In emacs lisp-mode + slime, typing
(format
will display the arguments of format in the minibuffer on the bottom.