宿題の2つのリストを比較する関数を書こうとしています。関数を実行すると、次のようになります;(cmp'(cat?x mat?x)'(cat bat mat bat))=> t;(cmp'(cat?x mat?x)'(cat bat mat sat))=>nil。つまり、最初のリストで?xに等しい場合、2番目の?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