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

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Thu Sep 30 10:36:26 EDT 2004

On Sep 30, 2004, at 9:12 AM, David J. Neu wrote:

> Many thanks to Matthias and Doug for their response!
>
> Matthias,
>
> To follow up on Daniel's question ... I'm writing a webapp that has an
> "executive" servlet that is similar to your execute procedure.  It
> handles the repetitive part of inserting records in a DBMS but needs a
> different web form, a differnt procedure to construct the insert
> statement, etc. for each table.
>
> For this application do I understand your response to Daniel as saying
> that dynamic-require is the way to go?

It depends on what you want to accomplish. If all these parameterized 
blocks of code are instantiated in the same program, use units and 
dynamic invocation. If you don't parameterize or if you don't 
instantiate them more than once, use module and dynamic-require.

-- Matthias


>
> Many thanks!
>
> Cheers,
> --David
>
>
> On Thu, Sep 30, 2004 at 08:25:07AM -0400, Matthias Felleisen wrote:
>>  For list-related administrative tasks:
>>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>>
>> 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 ****
>>>>
>>>>



Posted on the users mailing list.