Lisp:确定列表是否包含谓词
作为 Lisp 作业的一部分,我将对我找到的任何谓词使用 apply 或 funcall。我的问题(在课程作业中发现)是:我如何知道何时在参数列表中找到了谓词?我已经做了一些基本的谷歌搜索,但到目前为止什么也没得到。我们可以在作业中使用 Lisp 引用 - 甚至指向一个好的在线资源(也许是其中的特定页面)的指针也很棒!
As part of a homework assignment in Lisp, I am to use apply or funcall on any predicates I find. My question (uncovered in the coursework) is: how do I know when I've found a predicate in my list of arguments? I've done some basic google searching and come up with nothing so far. We're allowed to use Lisp references for the assignment - even a pointer to a good online resource (and perhaps a specific page within one) would be great!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
规范参考是 Common Lisp Hyperspec。
我不知道你的分配到底是什么,但你可以根据可能的谓词列表检查每个参数,或者确定你的参数是否是一个函数(
functionp
),如果你可以假设所有传入的函数都将是谓词。The canonical reference is the Common Lisp Hyperspec.
I don't know what your assignment is exactly, but you can either check each argument against a list of possible predicates, or perhaps determine if your argument is a function (
functionp
), if you can assume that all functions passed in would be predicates.添加到 Svante 的答案:我认为没有任何方法可以验证给定函数是否是谓词,就像您可以在静态类型语言中所做的那样。大多数 CL 实现确实提供了内省函数,例如 SBCL 的
sb-introspect:function-arglist
,它允许您检查是否只接受一个参数。不能保证该函数的行为是正常的,但总比没有好。To add to Svante's answer: I don't think there's any way to verify that a given function is a predicate as you might be able to do in a statically-typed language. Most CL implementations do provide introspection functions like SBCL's
sb-introspect:function-arglist
that will allow you to check to see that only one argument is accepted. It's no guarantee that the function's behavior is sane, but it may be better than nothing.