lisp - 如何使这个邻居功能?

标签 lisp common-lisp

我有这个代码:

(defparameter fc #\F)
(defparameter bc #\B)
(defparameter gap #\G)

(defun solp (seq)
    (if (eql fc (car seq))
        (not (if (listp (cdr seq))
                 (find bc (cdr seq))
                 (eql seq bc)))
        (solp (cdr seq))))

(defun heuristic (seq &optional (f 0)) 
    (if (eql nil seq) 
        0   
        (if (eql bc (car seq))
            (+ f (heuristic (cdr seq) f)) 
            (heuristic (cdr seq) (+ f 1)))))

(defun genneighbors (seq)

    ;seq == (fc fc gap bc bc) ===> neighbors == ( (gap fc fc bc bc)
    ;                                              (fc gap fc bc bc)
    ;                                               (fc fc bc gap)
    ;                                               (fc fc bc bc gap) )
    ;I can't figure out how to do this
)

我不知道如何编写 genneighbors 函数。如何在 gap 之前访问元素 (2 | 1) 个槽 如何生成所有四个可能的邻居?有人可以给我一些指示吗?

最佳答案

CLHS 章节中的所有函数 consessequences适用于列表。

另请注意,像这样命名全局变量是一种良好的编程风格(出于各种原因):*bc*、*fc* 和 *gap*。

另请注意,在较新的代码中,通常使用 FIRST 代替 CAR,使用 REST 代替 CDR。

关于lisp - 如何使这个邻居功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4295759/

相关文章:

lisp - Common Lisp : Are all functions built from the core functions, CAR、CDR、CONS 等?

common-lisp - 打印原始路径名结构

r - 像普通的 lisp 一样写 R

clojure - 在不定义宏的情况下将宏应用于窗体

lisp - 创建自定义列表反转

scheme - 如何在这里反转谓词?

Lisp - 消除语法中不可访问和非生产性元素的中间结果

lisp - 如何在 common lisp 中递归附加列表?

functional-programming - Common Lisp 中的 Eval

clojure - 来自除 CL 之外的其他语言的 Clojure 功能概述