[plt-scheme] dynamically varying the definitions used by a procedure
Hello all,
I have an application in which I'd like to be able to run a procedure,
(in the example below, the execute procedure) and to have the
variables, procedures, etc. that are used by the procedure vary - with
different definitions of the variables, procedures, etc. being loaded
from disk.
I was wondering if someone could let me know if there is a better way
to handle the following situation, than the way I'm doing it in the
following toy example?
The example contains three files: test.ss, euclidean.ss and hamming.ss.
Many thanks!
--David
;; *** start: test.ss ****
(define dbms
'((euclidean . "euclidean.ss")
(hamming . "hamming.ss")))
(define execute
(lambda (arequest)
(let ((lfilename (assq arequest dbms)))
(if lfilename
(begin
(load (cdr lfilename))
(printf "~a~n" (f x y)))
(error (format "request ~a not found~n" arequest))))))
(execute 'euclidean)
(execute 'hamming)
;; *** end: test.ss ****
;; *** start: euclidean.ss ****
(define x '(2 5 7))
(define y '(1 0 2))
(define square
(lambda (ax)
(* ax ax)))
(define f
(lambda (ax ay)
(sqrt
(+ (square (- (car ax) (car ay)))
(square (- (cadr ax) (cadr ay)))
(square (- (caddr ax) (caddr ay)))))))
;; *** end: euclidean.ss ****
;; *** start: hamming.ss ****
(define x '(1 0 0))
(define y '(1 0 1))
(define hamming
(lambda (ax ay)
(+ (- (car ax) (car ay)))
(- (cadr ax) (cadr ay))
(- (caddr ax) (caddr ay))))
;; *** end: hamming.ss ****