1

I have this code:

(setf prg '(+ 1 n)) ; define a very simple program
(print prg) ; print the program

I need to add more code so that when the above code is executed, it should set n to 1 and execute the program stored in variable prg.

1
  • Prg is a list of 3 values, you need to evaluate it in an environment where n is bound to 0. You can bind n globally to 0 with defvar. Why not use a function instead? Commented Jan 30, 2021 at 14:23

1 Answer 1

1

I think you want to do this:

(setf prg (lambda (n) + 1 n)) ; define a very simple program
(print (funcall prg 1))       ; print the program

In your example: (+ 1 n) is not a valid Common Lisp program.

EDIT: If you wanted to play with variables binding, you could also declare a variable:

(setf prg '(+ 1 n)) ; define a Common Lisp expression
(defparameter n 1)  ; bind a variable to the value 1
(print (eval prg))  ; evaluate the Common Lisp expression
> 2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.