[plt-scheme] dynamically varying the definitions used by a procedure
In this case, the goal is to parameterize expressions/definitions ( the
printf here ) over definitions in one and the same program. This is
what units are for. If David wants to split it over several different
runs, using modules is okay. -- Matthias
On Sep 30, 2004, at 6:15 AM, Daniel Silva wrote:
> When is it a good idea to use dynamic-require instead of units for
> problems like this one? The web-server servlet interface (a similar
> problem) uses both, right?
>
> Daniel
>
>
> On Wed, 29 Sep 2004 22:59:44 -0400, Matthias Felleisen
> <matthias at ccs.neu.edu> wrote:
>> For list-related administrative tasks:
>> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>>
>> 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 ****
>>
>>