Mysterious Racket: define: unbound identifier; In addition, the syntax transformer #% of the application is not connected: define

This program displays an error message:

define: unbound identifier; also, no #%app syntax transformer is bound in: define 

When pasted into REPL (more precisely, on the last line: (displayln (condition of the sentence is eval-clause))), it works. When launched in the definition window, it fails. I do not know why.

 #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*)) 

It is too:

 (define (eval-clause clause state) (eval `(let ,state ,(cons 'or clause)))) 

produces

 let: unbound identifier; also, no #%app syntax transformer is bound in: let 

This was my attempt to translate the following generic Lisp program: are generic Lisp involved here?

 ; (C) 2013 KIM Taegyoon ; 3-SAT problem ; https://groups.google.com/forum/#!topic/lisp-korea/sVajS0LEfoA (defvar *state* '((a nil) (bt) (ct) (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*)) 

And in 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*) 
+6
source share
2 answers

eval difficult in Racket. According to the Racquet Guide, 15.1.2, you need to connect to the current namespace as follows

 (define-namespace-anchor anc) (define ns (namespace-anchor->namespace anc)) 

and then add ns to each eval call:

 (define (eval-clause clause state) (for ([x state]) (eval `(define ,(first x) ,(second x)) ns)) (eval (cons 'or (map (curryr eval ns) clause)) ns)) 

Note that this is not necessary in the REPL, as described in the document mentioned above.

However, it is probably best to create a specific namespace for your definitions so that they do not mix with your own module definitions:

 (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*)) 

or, if you want to continue using true and false from racket/bool , define my-eval as follows:

 (define my-eval (let ((ns (make-base-namespace))) (parameterize ((current-namespace ns)) (namespace-require 'racket/bool)) (lambda (expr) (eval expr ns)))) 
+14
source

I would write a version of Common Lisp a little easier:

 (defun eval-clause (clause state) (loop for (var value) in state do (set var value)) (some #'eval clause)) 

The LOOP form is more descriptive (since we can get rid of CAR and NTH ), and EVAL can be directly used in the SOME function.

+3
source

All Articles