This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Problem setting value of symbol passed to function


Michael Bukin <M.A.Bukin@inp.nsk.su> writes:

 > Russ McManus <russell.mcmanus@gs.com> writes:
 > 
 > > (nobody) writes:
 > > 
 > > > I have tried to rewrite that code in Scheme (both scm5d0 and guile)
 > > > and failed.  I am a newbie in Scheme, so perhaps I'm missing
 > > > something obvious.
 > > 
 > > Can you post some code that doesn't work?  From the example you posted,
 > > I suspect that it is possible to do this with guile.
 > 
 > I have found solution which I can use while I have not learned enough
 > guile internals to make better one
 > 
 > (define (test s v)
 >   (if (not (symbol? s))
 >       (error "s must be symbol"))
 >   (if (not (integer? (eval s)))
 >       (error "s must evaluate to integer"))
 >   (let ((x (list 'set! s v)))
 >     (eval x)))
 > 
 > (define var 0)
 > var => 0
 > (test 'var 10)
 > var => 10
 > 
 > Now I will try to find more efficient ways to do that in C.

What about something like:

(define-macro (test s v)
  `(begin (if (not (symbol? ',s))
	      (error "s must be symbol"))
	  (if (not (integer? ,v))
	      (error "v must evaluate to integer"))
	  (set! ,s ,v)))

But, you probably want to also make sure that s is bound before doing
set!, or you'll also get an error for trying to set a variable that's
not bound.

-- 
Harvey J. Stein
BFM Financial Research
hjstein@bfr.co.il