Runtime evaluation/In an environment
Given a program in the language representing a function, evaluate it with the variable x (or another name if that is not valid) bound to a provided value, then evaluate it again with x bound to another provided value, then subtract the result of the first from the second and return or print it.
You are encouraged to solve this task according to the task description, using any language you may know.
Preferably, do so in a way which does not involve string manipulation of source code, and is plausibly extensible to a runtime-chosen set of bindings.
For more general examples and language-specific details, see Eval.
Common Lisp
<lang lisp> (defun eval-with-x (program a b)
(let ((at-a (eval `(let ((x ',a)) ,program))) (at-b (eval `(let ((x ',b)) ,program)))) (- at-b at-a)))
</lang>
<lang lisp> (eval-with-x '(exp x) 0 1) => 1.7182817 </lang lisp>
This version ensures that the program is compiled, once, for more efficient execution:
<lang lisp> (defun eval-with-x (program a b)
(let* ((f (compile nil `(lambda (x) ,program))) (at-a (funcall f a)) (at-b (funcall f b))) (- at-b at-a)))
</lang>