Emacs LISP - DeMorgan'列表

kal*_*eoh 4 lisp emacs demorgans-law

我在人工智能课程中,我们得到了一个写作的程序.该程序显然很简单,所有其他学生都用java编写.但是我知道可以在LISP中完成这项工作.好.减少打字.但是我已经读了一周关于LISP的事了,我很惊讶它.我决心学习更多,并且使用LISP不仅仅是本课程.我今年23岁,正在学习1958年成立的一门语言.这有点浪漫.我很有乐趣避免像瘟疫这样的鼠标垫.

他给出的例子讲述了整个程序.他指出他使用递归,而不是前卫.我明白这意味着什么,至少.

(rewrite '(or a (and b (not (or c d)))))

--> (OR A (AND B (AND (NOT C) (NOT D))))

(rewrite '(and a (or b (not (and c (and d e))))))

--> (AND A (OR B (NOT C) (OR (NOT D) (NOT E)))))
Run Code Online (Sandbox Code Playgroud)

我理解德摩根的法律.我只是不知道我应该如何处理这个!到目前为止我所拥有的是......令人尴尬.我的笔记本上写满了我试图把它画出来的页面.在最简单的情况下,我会给你最近的尝试:

(not (or a b))
Run Code Online (Sandbox Code Playgroud)

我想如果我能处理这个问题,我可能会好好处理剩下的事情.也许.我创建了一个名为boom的函数,上面的语句就是我称之为boomable列表的函数.

(defun boom (sexp)

  (let ((op (car (car (cdr sexp)))) 

    (operands (cdr (car (cdr sexp))))))

  (if (equal op 'and)

      (setcar sexp 'or)

    (setcar sexp 'and))

  (print operands)

  (print sexp))

                ;end boom
Run Code Online (Sandbox Code Playgroud)

我打印到最后进行调试.对列表操作数的更改并不反映原始sexp的变化(对我来说非常失望).

告诉我我的所作所为是假的,并指导我.

jki*_*ski 5

使用模式匹配的Emacs Lisp解决方案,基于Rainer Joswigs Common Lisp解决方案:

(defun de-morgan (exp)
  (pcase exp
    ((pred atom) exp)
    (`(not (and ,a ,b)) `(or ,(de-morgan `(not ,a))
                             ,(de-morgan `(not ,b))))
    (`(not (or ,a ,b)) `(and ,(de-morgan `(not ,a))
                             ,(de-morgan `(not ,b))))
    (x (cons (car x) (mapcar #'de-morgan (rest x))))))

(de-morgan '(not (or 1 2))) ; => (and (not 1) (not 2))
(de-morgan '(not (and 1 2))) ; => (or (not 1) (not 2))
(de-morgan '(or a (and b (not (or c d))))) ; => (or a (and b (and (not c) (not d))))
Run Code Online (Sandbox Code Playgroud)