我正在尝试编写一个比较两个家庭作业列表的函数。当函数运行时,它应该是这样的 ;(cmp '(cat ?x mat ?x) '(cat bat mat bat)) => t ;(cmp '(cat ?x mat ?x) '(cat bat mat坐))=> 无。这意味着在第一个列表中等于 ?x 和第二个 ?x 如果两者都指向相同的值,则返回 true。当我现在运行程序时,出现“将参数解析为特殊形式时出错:如果元素数量无效”,如果您能给我一些反馈,这是我的代码。谢谢。
;cmp algorithm
;1 if the both lists are empty return true
;2 if only one of the lists is empty return fasle
;3 compare first of the list1 and the first of list2
;if equal go on to the rest of the list with recursive call else return false
(defun cmp (list1 list2)
(setq y '())
(setq z '())
(defparameter *counter* 0)
(cond
((and (null list1) (null list2))
t
)
((or (null list1) (null list2))
nil
)
((or (eq (first list1) (first list2))
(eq (first list1) '?x) )
(cmp (rest list1) (rest list2) )
;if (first list is equal to '?x)
;set the counter to 1
;give the value of (first(rest list2)) to y
;if (first list is equal to '?x) again
;set the counter to 2
;give the value of (first (rest list2)) to z
;i need to compare y and z if eq return true
(if (eq (first list1) '?x)
(princ (first list1 ))
(princ (first(rest list2)))
(1+ *counter*)
(set y (first(rest list2)))
(if (= *counter* 2)
(set z (first (rest list2)))
)
)
(if (= y z) t)
)
(t
nil)
)
)
;(cmp ‘(cat ?x mat ?x) ‘(cat bat mat bat)) => t
;(cmp ‘(cat ?x mat ?x) ‘(cat bat mat sat)) => nil