Common lisp 调用deftype方法的公共Lisp

Common lisp 调用deftype方法的公共Lisp,common-lisp,predicate,deftype,Common Lisp,Predicate,Deftype,我没法让这一切顺利进行。我正在使用deftypeSametype(xy)方法定义一个谓词,该方法计算列表x和列表y的元素是否属于相同的类型,是否位于相同的位置。当我试图调用谓词进行测试时,问题就出现了。我收到一个错误错误:SameType未定义这是我的代码: (deftype SameType (x y) `(cond ((and (null x) (null y) T)) ( (and (numberp (car x)) (numberp (car y)))

我没法让这一切顺利进行。我正在使用
deftypeSametype(xy)
方法定义一个谓词,该方法计算列表x和列表y的元素是否属于相同的类型,是否位于相同的位置。当我试图调用谓词进行测试时,问题就出现了。我收到一个错误
错误:SameType未定义
这是我的代码:

(deftype SameType (x y)
`(cond 
    ((and (null x) (null y) T))
    (
        (and (numberp (car x)) (numberp (car y)))
        (SameType (cdr x) (cdr y) )
    )
    (
        (and (stringp (car x)) (stringp (car y)))
        (SameType (cdr x) (cdr y) )
    )
    (
        (and (atom (car x)) (atom (car y)))
        (SameType (cdr x) (cdr y) )
    )
    (T nil)
)
)

这就是我所说的

(SameType '(A B C 1 2 4 A) '('() G 2 5 6 A B))

我已经查看了各种在线资源,甚至这个站点上的相关问题。

deftype
可以用来定义类型,而不是谓词。例如,要定义仅包含整数的列表类型,可以编写如下内容:

(defun intlistp (l)
  "predicate to check if l is a list consisting only of integers"
  (and (listp l)               ; l is a list and
       (every #'integerp l)))  ; every element of l is an integer

(deftype integer-list ()
  "the type of list of integers"
  `(satisfies intlistp))
然后可以检查值是否满足此类型:

CL-USER> (typep '(1 2 3) 'integer-list)
T
CL-USER> (typep '(1 2.5 3) 'integer-list)
NIL
如果要根据定义检查两个列表是否具有相同的类型,则可以定义一个常规函数:

(defun same-type (l1 l2)
  "check if lists l1 and l2 have the same length and corresponding
   elements of the same CL type"
  (cond ((null l1)        ; if l1 is null
         (null l2))       ; returns true only if also l2 is null
        ((and (consp l1)  ; if l1 is a cons
              (consp l2)  ; and l2 is a cons too,
              (typep (car l1) (type-of (car l2)))) ; and their cars have the same CL type
         (same-type (cdr l1) (cdr l2))))) ; go recursively on their cdrs

CL-USER> (same-type '(1 a 3) '(2 b 4))
T
CL-USER> (same-type '(1 "a" 3) '(2 "b" 3))
T
CL-USER> (same-type '(1 a 3) '(2 b 4.5))
NIL
CL-USER> (same-type '(1 a 3) '(2 b 4 3))
NIL
CL-USER> (same-type '(1 2 (3 4)) '(1 6 (4 5)))
T
CL-USER> (same-type '(1 2 (3 4)) '(1 6 (4 5 6)))
T

请注意,正如您从上一个示例中所看到的,仅为列表的第一级检查类型。

deftype
可用于定义类型,而不是谓词。例如,要定义仅包含整数的列表类型,可以编写如下内容:

(defun intlistp (l)
  "predicate to check if l is a list consisting only of integers"
  (and (listp l)               ; l is a list and
       (every #'integerp l)))  ; every element of l is an integer

(deftype integer-list ()
  "the type of list of integers"
  `(satisfies intlistp))
然后可以检查值是否满足此类型:

CL-USER> (typep '(1 2 3) 'integer-list)
T
CL-USER> (typep '(1 2.5 3) 'integer-list)
NIL
如果要根据定义检查两个列表是否具有相同的类型,则可以定义一个常规函数:

(defun same-type (l1 l2)
  "check if lists l1 and l2 have the same length and corresponding
   elements of the same CL type"
  (cond ((null l1)        ; if l1 is null
         (null l2))       ; returns true only if also l2 is null
        ((and (consp l1)  ; if l1 is a cons
              (consp l2)  ; and l2 is a cons too,
              (typep (car l1) (type-of (car l2)))) ; and their cars have the same CL type
         (same-type (cdr l1) (cdr l2))))) ; go recursively on their cdrs

CL-USER> (same-type '(1 a 3) '(2 b 4))
T
CL-USER> (same-type '(1 "a" 3) '(2 "b" 3))
T
CL-USER> (same-type '(1 a 3) '(2 b 4.5))
NIL
CL-USER> (same-type '(1 a 3) '(2 b 4 3))
NIL
CL-USER> (same-type '(1 2 (3 4)) '(1 6 (4 5)))
T
CL-USER> (same-type '(1 2 (3 4)) '(1 6 (4 5 6)))
T
请注意,正如您在上一个示例中所看到的,仅为列表的第一级检查类型