【发布时间】: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