常见的LISP在Cond中使用Equals谓词

时间:2015-01-27 14:36:16

标签: common-lisp

在Common LISP中,我试图提供一个列表并创建一个数组:

(defun make (hand)
  (make-array '(5 2) 
              :initial-contents
              hand))

(defparameter array(make '((3 H)(2 H)(11 D)(8 C)(5 D))))

这似乎工作正常。我绊倒的地方是使用这个阵列。 我想比较数组每个位置的第二个字段。

即。 H eq H?是。 H eq D?没有。结束。

我不确定如何做到这一点。我试过了:

(cond ((eq 'aref hand 1 1) 'aref hand 0 1) (t)))

这不起作用。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:4)

eq有两个参数:

(eq 'h 'h)
=> T
(eq 'h 'd)
=> NIL

aref需要一些参数,具体取决于数组的排名 - 在您的情况下,三个:

(aref hand 1 1)
=> H
(aref hand 0 1)
=> H

你想要的是将后者作为前者的参数:

(eq (aref hand 1 1) (aref hand 0 1))
=> T

cond允许您检查一组条件并根据第一个条件返回一个值:(cond (condition-1 value-1) (condition-2 value-2) ...)

(cond ((eq (aref hand 1 1) (aref hand 0 1))
       "first and second are the same")
      ((eq (aref hand 2 1) (aref hand 0 1))
       "first and third are the same")
      (t
       "this is always true"))
=> "first and second are the same"
相关问题