[plt-scheme] dynamically varying the definitions used by a procedure

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Wed Sep 29 22:59:44 EDT 2004

Here is one way -- Matthias


;; *** start: fxy-sig.ss
(define-signature fxy^ (f x y))
;; *** end: fxy-sig.ss

;; *** start: test.ss ****
(load "fxy-sig.ss")

(define dbms
   '((euclidean . "euclidean.ss")
     (hamming . "hamming.ss")))

(define (execute arequest) (run (lookup arequest)))

(define run
   (lambda (lfilename)
     (invoke-unit/sig
      (compound-unit/sig
        (import)
        (link
         [FILE : fxy^ ((load  lfilename))]
         [MAIN : () ((unit/sig () (import fxy^)
                       (printf "~a~n" (f x y)))
                     FILE)])
        (export)))))

(define lookup
   (lambda (arequest)
     (cond
       [(assq arequest dbms) => cdr]
       [else (error (format "request ~a not found~n" arequest))])))

(execute 'euclidean)
(execute 'hamming)
;; *** end: test.ss ****

;; *** start: hamming.ss ****
(unit/sig fxy^
   (import)
   (define x '(1 0 0))

   (define y '(1 0 1))

   (define f ;hamming
     (lambda (ax ay)
       (+  (- (car ax)  (car ay)))
       (- (cadr ax)  (cadr ay))
       (- (caddr ax)  (caddr ay)))))
;; *** end: hamming.ss ****

;; *** start: euclidean.ss ****
(unit/sig fxy^
   (import)
   (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 ****


On Sep 29, 2004, at 7:31 PM, David J. Neu wrote:

>
> ;; *** 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 ****



Posted on the users mailing list.