[Top][All Lists]

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

Re: No gh_set_x()?

From: Dirk Esser
Subject: Re: No gh_set_x()?
Date: Fri, 13 Jul 2001 11:17:24 +0200 (MEST)

On Tue, 10 Jul 2001, Sam Tregar <address@hidden> wrote:

> Hey guys.  I'm working on a Perl binding for Guile 
> and I've run into a problem - I need a gh_set_x() but 
> it doesn't seem to exist.  I'm working on overloading 
> the special "+=" type arithmetic on the Perl side and I
> need to set the left-hand SCM to the value produced by 
> the addition.

Well, the others did get your point, I did not. Are you writing an
interface, which allows Perl code to use the Guile library (in the
way one would say: "...the Gtk library"), or is it just the other
way around? (Did I mention that english is not my first language --
must I mention, that english is not my first language... B-)?

Anyway, not having understood the problem won´t stop me from 
providing a solution...

I am not really sure, what you are going to do, but why do 
you insist on doing it in C -- implementing "+=" as a Scheme 
macro is straightforward and simple:

  (defmacro += (var . forms)
    (quasiquote(set! ,var (+ ,var ,@forms))))

If you prefer to have full C-style += semantics, including
the return value, try this instead:

  (defmacro += (var . forms)
    (quasiquote (begin (set! ,var (+ ,var ,@forms))

This can then be used from Scheme code like

  (let ((x 10) (y 0))
    (+= x y)
    (+= y 1)
    (display x)
    (display y))

Should print 10 for x and 1 for y (did not test it -- bug reports
wellcome... B-). C´s ++/-- operators can be implemented just as 
easily (and by reusing the 2nd definition above):

  (defmacro ++ (var)
     (quasiquote (+= ,var 1)))  ;; similar for --

The only thing, which cannot be implemented this way is the different
behvaiour of ++/-- depending on whether they are used in pre- or
postfix position.

BTW: When Guile supports the SFRI generalized set! implementation,
the macros above would also work in places like:

  (define a-vector (vector 0 1 2 3 4 5 6))
  (++ (vector-ref a-vector 4))

which would be somehting like a_vector[4]++; in C. 


GMX - Die Kommunikationsplattform im Internet.

GMX Tipp:

Machen Sie Ihr Hobby zu Geld bei unserem Partner 1&1!

reply via email to

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