这个程序产生一个错误:
define: unbound identifier;
also, no #%app syntax transformer is bound in: define
当粘贴到 REPL 中时(准确地说,最后一行:(displayln (eval-clause clause state))),它起作用了。在定义窗口中运行时,它会失败。我不知道为什么。
#lang racket
(define *state* '((a false) (b true) (c true) (d false)))
(define *clause* '(a (not b) c))
(define (eval-clause clause state)
(for ([x state])
(eval `(define ,(first x) ,(second x))))
(eval (cons 'or (map eval clause))))
(displayln (eval-clause *clause* *state*))
这也是:
(define (eval-clause clause state)
(eval `(let ,state ,(cons 'or clause))))
产生
let: unbound identifier;
also, no #%app syntax transformer is bound in: let
这是我翻译以下 Common Lisp 程序的尝试:Common Lisp wins here?
; (C) 2013 KIM Taegyoon
; 3-SAT problem
; https://groups.google.com/forum/#!topic/lisp-korea/sVajS0LEfoA
(defvar *state* '((a nil) (b t) (c t) (d nil)))
(defvar *clause* '(a (not b) c))
(defun eval-clause (clause state)
(dolist (x state)
(set (car x) (nth 1 x)))
(some #'identity (mapcar #'eval clause)))
(print (eval-clause *clause* *state*))
并且在 Paren :
(set *state* (quote ((a false) (b false) (c true) (d false))))
(set *clause* (quote (a (! b) c)))
(defn eval-clause (clause state)
(for i 0 (dec (length state)) 1
(set x (nth i state))
(eval (list set (nth 0 x) (nth 1 x))))
(eval (cons || clause)))
(eval-clause *clause* *state*)
最佳答案
eval
在 Racket 中很棘手。根据 Racket 指南 15.1.2,您需要按如下方式挂接到当前命名空间
(define-namespace-anchor anc)
(define ns (namespace-anchor->namespace anc))
然后在每次调用 eval
时添加 ns
:
(define (eval-clause clause state)
(for ([x state])
(eval `(define ,(first x) ,(second x)) ns))
(eval (cons 'or (map (curryr eval ns) clause)) ns))
请注意,正如上面引用的文档中所述,这在 REPL 中不是必需的。
但是,为您的定义创建一个特定的命名空间可能是一个更好的主意,这样它们就不会与您自己的模块定义混淆:
(define my-eval
(let ((ns (make-base-namespace)))
(lambda (expr) (eval expr ns))))
(define *state* '((a #f) (b #t) (c #t) (d #f)))
(define *clause* '(a (not b) c))
(define (eval-clause clause state)
(for ([x state])
(my-eval `(define ,(first x) ,(second x))))
(my-eval (cons 'or (map my-eval clause))))
(displayln (eval-clause *clause* *state*))
或者,如果您想继续使用 racket/bool
中的 true
和 false
,请定义 my-eval
如下;
(define my-eval
(let ((ns (make-base-namespace)))
(parameterize ((current-namespace ns))
(namespace-require 'racket/bool))
(lambda (expr) (eval expr ns))))
关于lisp - Mysterious Racket 错误 : define: unbound identifier; also, 没有#%app 语法转换器绑定(bind)在:define,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20778926/