【问题标题】:When I run this, it states that the list constraints is unbound. Why is that?当我运行它时,它表明列表约束是未绑定的。这是为什么?
【发布时间】:2016-09-06 19:59:45
【问题描述】:
(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)))

combinations 函数为每个棒球运动员创建姓名、魅力和位置的所有组合。

(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))))

主要功能用于解决一个不知道玩家位置和魅力的问题。主要功能列出所有可能的球员组合、他们的魅力和他们的棒球位置。然后它声明一个约束列表,每个列表都在开头声明 no,以指示 no 之后的两个值不应以任何组合形式出现。进行循环以从约束列表中获取一个约束。约束的汽车本身就是一个列表。 Main 然后使用 remove-l 函数来消除不符合约束的组合。 remove-l 然后返回一个新的 m-list,其组合比以前少

(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))))

这里的Remove-l 函数返回一个新列表,其中大部分组合与以前相同。约束列表中的一个约束用于消除某些组合。

(defvar  *data* nil) 

忽略

(defun add-player (player)
  (push player *data*))

忽略

(defun dump-data ()
  (dolist (cd *data*)
   (format t "~{~a:~10t~a~%~}~%" cd)))

忽略

【问题讨论】:

  • 可能是因为您将“约束”拼写为“约束”?
  • 除此之外,您似乎没有在任何地方定义constraints
  • SETF 和 SETQ 不定义变量。你的代码中有无数个未定义的变量。这是为什么呢?

标签: list nested lisp common-lisp prefix


【解决方案1】:

Xach 已经指出了 cmets 中的拼写错误,但我想我会针对您的代码添加一些 cmets。

您不应使用SETQSETF 定义变量。这些应该仅用于将值设置为已定义的变量。对局部变量使用LET/LET*,对全局变量使用DEFVAR/DEFPARAMETER

循环遍历列表也是很常见的事情,它有内置的构造:DOLIST 和扩展的LOOPs,您可以使用FOR element IN list

在修复这些并为您的REMOVE-L 添加一些更好的缩进之后,它看起来像这样:

(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))))))

这仍然存在一些问题。请注意AND 中只有一个表单,而NOT 有两个。 = 用于数值相等,因此您应该使用 NOTNULL 来检查某些内容是否不正确。然后当然是CONS 没有破坏性的问题;您必须将其返回值设置为某个地方。就像现在一样,循环不做任何事情。您可以使用PUSH 将元素添加到列表中。

修复这些,你会得到这样的东西:

(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)))))

您可以通过将两个约束分配给变量(使用LETDESTRUCTURING-BIND)来进一步改进它,而不是每次迭代调用NTH 两次。 但是,过滤列表也是一件很常见的事情,您的REMOVE-L 可以很容易地用内置的REMOVE-IF 表示。您可以将您的 MAIN 更改为以下内容:

(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))

编辑: 现在我记起来了,Common Lisp 还有一个内置函数SUBSETP 来检查一个列表是否是另一个列表的子集(不考虑顺序)。这样你就不需要解构约束列表了。

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

这是一个使用currying的好地方,它不是内置的,但如果你安装了Quicklisp,你可以使用Alexandria的实现,或者你可以自己写一个简单的:

(defun curry (function &rest arguments)
  (lambda (&rest more)
    (multiple-value-call function (values-list arguments) (values-list more))))

(defun main ()
  (let ((m-list ...)
        (constraints ...))
    (dolist (constraint constraints m-list)
      (setf m-list (remove-if (curry #'subsetp (cdr constraint))
                              m-list)))))

【讨论】:

  • 更不用说= 用于数值相等,nil 是很多东西,但不是数字。 编辑 哇!你已经提到了。
猜你喜欢
  • 1970-01-01
  • 2015-09-07
  • 2020-04-30
  • 2019-12-17
  • 1970-01-01
  • 2012-03-15
  • 1970-01-01
  • 2022-06-15
  • 1970-01-01
相关资源
最近更新 更多