小编Mic*_*lle的帖子

如何在 Lisp 中一次生成一个列表中元素的所有排列?

我已经有了生成元素列表的所有排列的代码。然而,我意识到,如果我想操作生成的列表,我需要遍历这个列表。该列表可能会很大,因此维护成本很高。我想知道是否有一种方法可以通过每次调用生成排列,以便我可以检查列表是否与我需要的匹配,如果不匹配,我将生成下一个排列。(每次该函数都会一次返回一个列表。)

我的代码:

(defun allPermutations (list) 
  (cond
     ((null list)  nil) 
     ((null (cdr list))  (list list)) 
     (t  (loop for element in list 
               append (mapcar (lambda (l) (cons element l))
                              (allPermutations (remove element list))))))) 
Run Code Online (Sandbox Code Playgroud)

lisp list generator permutation common-lisp

3
推荐指数
2
解决办法
1225
查看次数

如何在lisp中生成一个笛卡尔积?

这是我生成笛卡尔积的代码:

(defun cartesian-product (LIST)
  (LOOP FOR X IN LIST
    NCONC
        (LOOP FOR Y IN LIST
         COLLECT (LIST X Y))))
Run Code Online (Sandbox Code Playgroud)

我尝试输出其中一个笛卡儿产品:

(defun cartesian-product-generator (CALLBACK LIST)
  (LOOP FOR X IN LIST
    NCONC
        (LOOP FOR Y IN LIST
        DO(FUNCALL CALLBACK (LIST X Y)))))
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用以下测试时出现错误:

(cartesian-product-generator '(A B C))

Error: Too few arguments in call to #<Compiled-function cartesian-product-generator #x30200097E60F>:
       1 argument provided, at least 2 required.  While executing: cartesian-product-generator, in process listener(1).
Run Code Online (Sandbox Code Playgroud)

我是LISP的新手,想知道为什么会出现错误以及如何修复此错误.最终,我希望每次调用函数输出每个笛卡尔积.

例如,如果列表包含((1 1) (1 2) (2 1) (2 2)).我想生成(1 …

lisp loops generator common-lisp cartesian-product

1
推荐指数
1
解决办法
542
查看次数

如果在Lisp中没有定义超类状态,这意味着什么?

我的超类定义如下:

(defclass missionary-state (state)
  ((missionary-left :initarg :missionary-left :initform nil :accessor missionary-left
    :documentation "the number of missionaries on the left side of the river")
   (missionary-right :initarg :missionary-right :initform nil :accessor missionary-right
    :documentation "the number of missionaries on the right side of the river")
   (cannibal-left :initarg :cannibal-left :initform nil :accessor cannibal-left
    :documentation "the number of cannibals on the left side of the river")
   (cannibal-right :initarg :cannibal-right :initform nil :accessor cannibal-right
     :documentation "the number of cannibals on the right side of the river") …
Run Code Online (Sandbox Code Playgroud)

class common-lisp clos superclass

0
推荐指数
1
解决办法
91
查看次数