问题描述
我是 Common Lisp 的新手,尤其是 CLOS。我在名为 Nyxt 的包中使用 REPL。
Nyxt 旨在成为一个可无限扩展的浏览器。因此,用户可以在程序运行时更改代码和/或创建扩展。这是设计上的实时可入侵性。我的问题与 Nyxt 包无关,但由于它发生在包内,我认为最好提供更多背景信息。
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>
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)
是 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/>)
(这是一个 list);而 small-list
包含 element #<QURI.URI.HTTP:URI-HTTPS https://ambrevar.xyz/>.
请记住,last
返回列表的最后一个 cons(如果提供可选参数,则返回最后一个 n
conses)。你可以这样做:(find (car (last small-list)) small-list)
。
另一种可能性是使用 test
和 key
关键字参数:(find (last small-list) small-list :test #'equal :key #'list)
。但是,我不确定什么时候我更喜欢这个而不是第一个解决方案。
您的问题是您认为普通 lisp 中的 <ListView.ItemTemplate>
<DataTemplate>
<TextCell TextColor="black" Text="{Binding ResourceName}" />
</DataTemplate>
</ListView.ItemTemplate>
返回列表的最后一个元素。但是如果你仔细看,它会返回最后一个打包到列表中的元素!
last
是您认为 (car (last small-list))
实际所做的。
last
会起作用!
(defparameter blog (car (last small-list)))
(find blog small-list :test #'equal)
不是正确的测试。因为 (equal blog (last small-list))
你之前定义了使用
blog
因此当然必须是 (last small-list)
。