我是 Common Lisp 的新手,尤其是 CLOS。我在名为 Nyxt 的包中使用 REPL。
Nyxt被设计成一个可无限扩展的浏览器。因此,用户可以在程序运行时更改代码和/或创建扩展。这是设计的实时黑客能力。我的问题与 Nyxt 包无关,但由于它发生在包内,我认为提供更多背景信息会更好。
我不明白函数 find
在这个具体案例中的行为。
我有这个代表 URL 的实例的小列表:
NYXT> small-list
(#<QURI.URI.HTTP:URI-HTTPS https://duckduckgo.com/?q=google+analytics&ia=web>
#<QURI.URI.HTTP:URI-HTTPS https://duckduckgo.com/l/?uddg=https%3A%2F%2Fanalytics.withgoogle.com%2F¬rut=duckduck_in>
#<QURI.URI.HTTP:URI-HTTPS https://en.wikipedia.org/wiki/CAPTCHA>
#<QURI.URI:URI about:blank> #<QURI.URI.HTTP:URI-HTTPS https://ambrevar.xyz/>)
然后,我将列表的第三个元素定义为变量:
NYXT> (defparameter wikipedia-page (third small-list))
WIKIPEDIA-PAGE
NYXT> wikipedia-page
#<QURI.URI.HTTP:URI-HTTPS https://en.wikipedia.org/wiki/CAPTCHA>
好的,如果我尝试在列表中找到维基百科页面,它会按预期工作:
NYXT> (find wikipedia-page small-list :test #'equal)
#<QURI.URI.HTTP:URI-HTTPS https://en.wikipedia.org/wiki/CAPTCHA>
现在,让我将另一个实例绑定(bind)到一个变量:
NYXT> (defparameter blog (last small-list))
BLOG
NYXT> blog
(#<QURI.URI.HTTP:URI-HTTPS https://ambrevar.xyz/>)
问题是当我试图找到它时:
NYXT> (find blog small-list :test #'equal)
NIL
现在对我来说是最奇怪的部分,平等测试有效:
NYXT> (equal blog (last small-list))
T
谁能帮帮我?为什么 find 不适用于 blog
案例?这与 CLOS 相关吗?应该如何比较对象?
谢谢
最佳答案
给定small-list
如问题中所定义,(last small-list)
是列表 (#<QURI.URI.HTTP:URI-HTTPS https://ambrevar.xyz/>)
.所以,当然(find (last small-list) small-list)
应该会返回 nil
, 自 small-list
不包含元素 (#<QURI.URI.HTTP:URI-HTTPS https://ambrevar.xyz/>)
(这是一个列表);相反 small-list
包含元素 #<QURI.URI.HTTP:URI-HTTPS https://ambrevar.xyz/>.
记住 last
返回列表的最后一个 cons(如果提供可选参数,则返回最后一个 n
cons)。你可以这样做:(find (car (last small-list)) small-list)
.
另一种可能性是使用 test
和 key
关键字参数:(find (last small-list) small-list :test #'equal :key #'list)
.但是,我不确定什么时候我更喜欢这个而不是第一个解决方案。
https://stackoverflow.com/questions/68239500/