gpt4 book ai didi

common-lisp - 了解 Common Lisp 中的类型说明符

转载 作者:行者123 更新时间:2023-12-04 15:23:35 24 4
gpt4 key购买 nike

我在 LispWorks Hyper Spec 中找到了一个很好的类型检查示例,但是“类型说明符”链接仅指向一个词汇表而不是表示法,而且我对语法有点困惑。

(check-type n (integer 0 *) "a positive integer")什么(integer 0 *)意思?我认为这意味着从 0 到无穷大的包含范围,但是是这样吗?

最佳答案

是的,您可以在普通 lisp 中使用类型说明符,如果您的编译器选择使用它们,它们会非常强大。虽然您可能会发现 check-type 的用途,但最常见的类型规范以 declarations 的形式出现。 .

declare表达式不仅用于类型而且它有许多 declaration identifiers并且常见的 lisp 实现实际上可以自由添加它们自己的。

您感兴趣的位是 'types'更具体地说'Type Specifiers' .该页面将为您提供各种指定类型的方法的详细信息,包括您在问题中提到的方式。

再次注意,您的实现不必使用它可以忽略它们的声明! Here is some more info on that.

对于一些示例代码,这里是让我了解其工作原理的示例。 Here更多 here .

来自 4.2.3 Type Specifiers :

If a type specifier is a list, the car of the list is a symbol, and the rest of the list is subsidiary type information. Such a type specifier is called a compound type specifier. Except as explicitly stated otherwise, the subsidiary items can be unspecified. The unspecified subsidiary items are indicated by writing *. For example, to completely specify a vector, the type of the elements and the length of the vector must be present.

(vector double-float 100)

The following leaves the length unspecified:

(vector double-float *)

The following leaves the element type unspecified:

(vector * 100)

关于common-lisp - 了解 Common Lisp 中的类型说明符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18120542/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com