List 当我运行此命令时,它表示列表约束已解除绑定。为什么呢?

List 当我运行此命令时,它表示列表约束已解除绑定。为什么呢?,list,nested,lisp,common-lisp,prefix,List,Nested,Lisp,Common Lisp,Prefix,“组合”功能为每个棒球运动员创建名称、魅力和位置的所有组合 (defun combinations (&rest lists) (if (car lists) (mapcan (lambda (inner-val)(mapcar (lambda (outer-val) (cons outer-val inner-val)) (car lists))) (apply #'combinations (cdr lists))) (list nil))) 主要功能用于解决一个问题,即不知道玩家

“组合”功能为每个棒球运动员创建名称、魅力和位置的所有组合

(defun combinations (&rest lists) (if (car lists) (mapcan (lambda (inner-val)(mapcar (lambda (outer-val) (cons outer-val inner-val)) (car lists))) (apply #'combinations (cdr lists))) (list nil)))
主要功能用于解决一个问题,即不知道玩家的位置和魅力。主要功能是列出所有可能的球员组合、他们的魅力和他们的棒球位置。然后,它声明一个约束列表,每个列表在开头都声明no,以指示no后面的两个值不应该是任何组合。循环是为了从约束列表中获取一个约束。约束的car本身就是一个列表。然后,Main使用remove-l函数消除不符合约束的组合。然后,Remove-l返回一个新的m-list,其组合比以前更少

(defun main()
  (setq m-list (combinations '(Blacket Bluet Browning Greenfield Whitehall)'(four-lear-clover penny rabbit-foot ribbon silver-dollar) '(center-    field first-base right-field short-stop third-base)))
  (setq contraints  (list '(no Browning penny) '(no Browning silver-dollar) '(no Browning right-field) '(no Browning center-field) '(no Bluet center-field) '(no Bluet right-field) '(no Greenfield first-base) '(no Greenfield short-stop)
    '(no Greenfield third-base) '(no Whitehall center-field) '(no Whitehall right-field) '(no Greenfield four-leaf-clover) '(no Greenfield penny) '(no Whitehall four-lear-clover) '(no Whitehall penny) 
    '(no Blacket four-leaf-clover) '(no Blacket penny) '(no Blacket first-base) '(no Blacket third-base) '(no Blacket ribbon) '(no Bluet ribbon) '(no center-field rabbit-foot)))
  (loop  
   (setf n-constraint (car constraints))
   (setf m-list (remove-l m-list n-constraint))
   (setf constraints (cdr constraints))
   (when (null constraints) (return m-list))))
Remove-l函数在这里提供返回一个新列表,其中包含与以前相同的大多数组合。“约束”列表中的一个约束用于消除某些组合

(defun remove-l (a b)
  (setf n-list '())
  (loop 
    (setf sample (car a))
    (when (and (not (= (find (nth 1 b) sample) nil) (= (find (nth 2 b)sample) nil))) (cons sample (cons n-list nil)))
(setf a (cdr a))(when (null a) (return n-list))))
忽略

忽略

忽略


Xach已经指出了注释中的拼写错误,但我想我应该添加一些关于代码的注释

不应使用
SETQ
SETF
定义变量。这些值只能用于设置已定义变量的值。用于局部变量或全局变量

在列表上循环也是一件很常见的事情,它有内置的结构:在扩展的
循环中,您可以对列表中的元素使用

在修复这些问题并向
REMOVE-L
添加一些更好的缩进后,它将如下所示:

(defun dump-data ()
  (dolist (cd *data*)
   (format t "~{~a:~10t~a~%~}~%" cd)))
(defun remove-l (a b)
  (let ((n-list '()))
    (dolist (sample a n-list) ; That N-LIST is the return value from the loop
      (when (and (not (= (find (nth 1 b) sample)
                         nil)
                      (= (find (nth 2 b) sample)
                         nil)))
        (cons sample (cons n-list nil))))))
(defun remove-l (a b)
  (let ((n-list '()))
    (dolist (sample a n-list)
      (when (and (not (find (nth 1 b) sample))
                 (not (find (nth 2 b) sample)))
        (push sample n-list)))))
这仍然存在一些问题。请注意,
只有一种形式,而
不是
有两种形式
=
表示数字相等,因此您应该使用
NOT
或检查某些内容是否为非真。当然还有一个问题,
CONS
不是破坏性的;您必须将其返回值设置为某个位置。现在,循环没有任何作用。您可以使用将元素添加到列表中

修复这些问题时,您会遇到如下情况:

(defun dump-data ()
  (dolist (cd *data*)
   (format t "~{~a:~10t~a~%~}~%" cd)))
(defun remove-l (a b)
  (let ((n-list '()))
    (dolist (sample a n-list) ; That N-LIST is the return value from the loop
      (when (and (not (= (find (nth 1 b) sample)
                         nil)
                      (= (find (nth 2 b) sample)
                         nil)))
        (cons sample (cons n-list nil))))))
(defun remove-l (a b)
  (let ((n-list '()))
    (dolist (sample a n-list)
      (when (and (not (find (nth 1 b) sample))
                 (not (find (nth 2 b) sample)))
        (push sample n-list)))))
您可以通过将这两个约束分配给变量(使用
LET
或)而不是每次迭代调用
NTH
两次来进一步改进它。 然而,过滤列表也是一件非常常见的事情,您的
REMOVE-L
可以很容易地用内置代码表示。您可以将
MAIN
更改为以下内容:

(defun dump-data ()
  (dolist (cd *data*)
   (format t "~{~a:~10t~a~%~}~%" cd)))
(defun remove-l (a b)
  (let ((n-list '()))
    (dolist (sample a n-list) ; That N-LIST is the return value from the loop
      (when (and (not (= (find (nth 1 b) sample)
                         nil)
                      (= (find (nth 2 b) sample)
                         nil)))
        (cons sample (cons n-list nil))))))
(defun remove-l (a b)
  (let ((n-list '()))
    (dolist (sample a n-list)
      (when (and (not (find (nth 1 b) sample))
                 (not (find (nth 2 b) sample)))
        (push sample n-list)))))
编辑:现在我记起了,Common Lisp还有一个内置函数,用于检查列表是否是另一个列表的子集(不考虑顺序)。这样,就不需要对约束列表进行分解

(defun main ()
  (let ((m-list ...) ; I left out the long lists. Fill them in.
        (constraints ...))
    ;; This uses LOOPs destructuring assignment. The underscore is
    ;; just an unused variable that holds the NO in each constraint.
    ;; CONSTRAINT-1 and -2 hold the two symbols.
    (loop for (_ constraint-1 constraint-2) in constraints
          do (setf m-list (remove-if (lambda (sample)
                                       ;; I used MEMBER instead of FIND.
                                       ;; It doesn't really matter, but
                                       ;; MEMBER communicates intent better.
                                       (and (member constraint-1 sample)
                                            (member constraint-2 sample)))
                                     m-list)))
    m-list))
这将是一个很好的使用场所,它不是内置的,但是如果您已经安装了,您可以从中使用实现,也可以自己编写一个简单的实现:

(defun main ()
  (let ((m-list ...)
        (constraints ...))
    (dolist (constraint constraints m-list)
      (setf m-list (remove-if (lambda (sample)
                                (subsetp (cdr constraint)
                                         sample))
                              m-list)))))

Xach已经指出了注释中的拼写错误,但我想我应该添加一些关于代码的注释

不应使用
SETQ
SETF
定义变量。这些值只能用于设置已定义变量的值。用于局部变量或全局变量

在列表上循环也是一件很常见的事情,它有内置的结构:在扩展的
循环中,您可以对列表中的元素使用

在修复这些问题并向
REMOVE-L
添加一些更好的缩进后,它将如下所示:

(defun dump-data ()
  (dolist (cd *data*)
   (format t "~{~a:~10t~a~%~}~%" cd)))
(defun remove-l (a b)
  (let ((n-list '()))
    (dolist (sample a n-list) ; That N-LIST is the return value from the loop
      (when (and (not (= (find (nth 1 b) sample)
                         nil)
                      (= (find (nth 2 b) sample)
                         nil)))
        (cons sample (cons n-list nil))))))
(defun remove-l (a b)
  (let ((n-list '()))
    (dolist (sample a n-list)
      (when (and (not (find (nth 1 b) sample))
                 (not (find (nth 2 b) sample)))
        (push sample n-list)))))
这仍然存在一些问题。请注意,
只有一种形式,而
不是
有两种形式
=
表示数字相等,因此您应该使用
NOT
或检查某些内容是否为非真。当然还有一个问题,
CONS
不是破坏性的;您必须将其返回值设置为某个位置。现在,循环没有任何作用。您可以使用将元素添加到列表中

修复这些问题时,您会遇到如下情况:

(defun dump-data ()
  (dolist (cd *data*)
   (format t "~{~a:~10t~a~%~}~%" cd)))
(defun remove-l (a b)
  (let ((n-list '()))
    (dolist (sample a n-list) ; That N-LIST is the return value from the loop
      (when (and (not (= (find (nth 1 b) sample)
                         nil)
                      (= (find (nth 2 b) sample)
                         nil)))
        (cons sample (cons n-list nil))))))
(defun remove-l (a b)
  (let ((n-list '()))
    (dolist (sample a n-list)
      (when (and (not (find (nth 1 b) sample))
                 (not (find (nth 2 b) sample)))
        (push sample n-list)))))
您可以通过将这两个约束分配给变量(使用
LET
或)而不是每次迭代调用
NTH
两次来进一步改进它。 然而,过滤列表也是一件非常常见的事情,您的
REMOVE-L
可以很容易地用内置代码表示。您可以将
MAIN
更改为以下内容:

(defun dump-data ()
  (dolist (cd *data*)
   (format t "~{~a:~10t~a~%~}~%" cd)))
(defun remove-l (a b)
  (let ((n-list '()))
    (dolist (sample a n-list) ; That N-LIST is the return value from the loop
      (when (and (not (= (find (nth 1 b) sample)
                         nil)
                      (= (find (nth 2 b) sample)
                         nil)))
        (cons sample (cons n-list nil))))))
(defun remove-l (a b)
  (let ((n-list '()))
    (dolist (sample a n-list)
      (when (and (not (find (nth 1 b) sample))
                 (not (find (nth 2 b) sample)))
        (push sample n-list)))))
编辑:现在我记起了,Common Lisp还有一个内置函数,用于检查列表是否是另一个列表的子集(不考虑顺序)。这样,就不需要对约束列表进行分解

(defun main ()
  (let ((m-list ...) ; I left out the long lists. Fill them in.
        (constraints ...))
    ;; This uses LOOPs destructuring assignment. The underscore is
    ;; just an unused variable that holds the NO in each constraint.
    ;; CONSTRAINT-1 and -2 hold the two symbols.
    (loop for (_ constraint-1 constraint-2) in constraints
          do (setf m-list (remove-if (lambda (sample)
                                       ;; I used MEMBER instead of FIND.
                                       ;; It doesn't really matter, but
                                       ;; MEMBER communicates intent better.
                                       (and (member constraint-1 sample)
                                            (member constraint-2 sample)))
                                     m-list)))
    m-list))
这将是一个很好的使用场所,它不是内置的,但是如果您已经安装了,您可以从中使用实现,也可以自己编写一个简单的实现:

(defun main ()
  (let ((m-list ...)
        (constraints ...))
    (dolist (constraint constraints m-list)
      (setf m-list (remove-if (lambda (sample)
                                (subsetp (cdr constraint)
                                         sample))
                              m-list)))))

也许是因为您将“约束”拼写为“约束”?除此之外,您似乎没有在任何地方定义
constraints
anywhere。SETF和SETQ没有定义变量。代码中有无数未定义的变量。这是为什么?可能是因为您将“约束”拼写为“约束”?除此之外,您似乎没有在任何地方定义
约束。代码中有无数未定义的变量。为什么会这样?更不用说
=
表示数字相等,
nil
表示很多东西,但不是一个数字。编辑Doh!你已经提到了。更不用说,
=
表示数字相等,
nil
表示很多东西,但不是一个数字。编辑Doh!你已经提到了。