guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Divide by zero or how to overload a function


From: Harvey J. Stein
Subject: Re: Divide by zero or how to overload a function
Date: 22 Oct 2001 16:28:19 -0400

Brett Viren <address@hidden> writes:

 > Robert A. Uhl writes:
 >  > (set! / (let ((real-divide /)) (lambda your code here)))
 > 
 > Yes, thanks, this works okay:
 > 
 > (set! / (let ((the-real-/ /)) 
 >           (lambda (a . l) 
 >             (letrec ((div (lambda (a l)  
 >                             (cond ((equal? l '()) a) 
 >                                   ((zero? (car l)) 0) 
 >                                   (else  
 >                                    (set! a (the-real-/ a (car l))) 
 >                                    (div a (cdr l))))))) 
 >               (div a l))))) 
 > 
 > and allows the code to be loaded more than once.  Although, one
 > slightly negative thing is that after each subsequent reloading of the
 > code, processing goes through the fake "/" more and more times in
 > order to get to the real "/".  But, I can live with this.

1. What about doing something like:

(if (not (symbol-bound? 'real-divide)
   (define real-divide /)
   (define / (lambda a . l) ...)))

Then you can reload it as much as you want, although, as someone else
mentioned, you shouldn't keep reloading this to begin with.

2. What about doing something like:

(let* ((real-divide /)
       (/ (lambda (a . l) ...)))
  <Interpret user code here>
)

to interpret the user's code in the appropriate context without
ruining the global definition of "/"?

-- 
Harvey Stein
Bloomberg LP
address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]