3

I am working on a program that generates program (Genetic Programming). I build at runtime an s-expression and today I use eval like this:

(defvar *program* '(+ x 1))
(defvar x 10)
(eval *program*) ;; returns 11

Evaluation is done for multiple x and I want to compile the s-expression into a function at runtime and then call it for multiple x to improve performance.

I can't figure out how to do it and I will appreciate some help. Here is what I have:

;;;; This is not working code
(defmacro compile-program (args &body body)
  (compile nil `(lambda (,@args)
                  (declare (ignorable ,@args))
                  (progn ,@body))))

(funcall (compile-program (x) *program*) 10) ;; returns (+ X 1)
(funcall (compile-program (x) (+ x 1)) 10) ;; returns 11

Edit: Thx to @RainerJoswig I made the following modifications and it works:

;;;; Working Code
(defvar *program* '(+ x 1))

(defun compile-program (args program)
  (compile nil `(lambda ,args
                   (declare (ignorable ,@args))
                    ,program)))

(funcall (compile-program '(x) *program*) 10) ;; returns 11

1 Answers1

2

Thx to @RainerJoswig and @coredump I made the following modifications and it works:

;;;; Working Code
(defvar *program* '(+ x 1))

(defun compile-program (args program)
  (compile nil `(lambda ,args
                   (declare (ignorable ,@args))
                    ,program)))

(funcall (compile-program '(x) *program*) 10) ;; returns 11