在LISP中,您是否可以构造一个“ check-type”,如果该值不是所有整数键和值的标签,则会引发错误?
假设我有一个功能:
(defun distribution-to-list (distribution)
(check-type distribution hash-table)
(loop for key being each hash-key of distribution
using (hash-value value) nconc (loop repeat value collect key)))
我想确保至少传递的哈希桌子的所有值都是整数,因为我使用它们将其重复为大列表。在内部循环之前,有什么方法可以使用 check-type
?还是在尝试重复
字符串时,让内部循环宏宏大误差会是足够好的练习? (或任何非整数类型)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您可以编写一个可以检查值是否可以接受的函数,则可以使用满足构建类型规范符,例如
(满足可接受的)
。例如,带有的速记(满足All-Integer-keys-p),您可能会发现更多可读性:
If you can write a function that can check whether a value is acceptable, then you can use satisfies to construct a type specifier, such as
(satisfies is-acceptable)
. E.g.,With deftype, you can define a type as shorthand for
(satisfies all-integer-keys-p)
, which you may find more readable: