unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: "Maciek Godek" <pstrychuj@gmail.com>
To: "Kjetil S. Matheussen" <k.s.matheussen@notam02.no>
Cc: guile-user@gnu.org
Subject: Re: References/locations
Date: Mon, 11 Aug 2008 01:00:21 +0200	[thread overview]
Message-ID: <e2ceda030808101600k2469efa7k56800fa8d480d3c@mail.gmail.com> (raw)
In-Reply-To: <Pine.LNX.4.64.0808082021001.5443@ttleush>

Kjetil:

>> Hi,
>> it's me again, asking silly questions.
>
> Not at all, your questions are very good. :-)

That's werid. Every time I write a post I've got the
strange feeling that I'm thinking in the way that
should be forbidden :)

I can give you the background of this idea.
I've been trying to implement a vector-based
object-oriented system (I'm rather reluctant towards
goops). I imagine that it could be used in the
following manner (borrowed from C++):

(define sphere
  (class
   () ;; public slots
   (x y radius) ;; private slots
   ((move (dx dy) ;; methods
	  (set! x (+ x dx))
	  (set! y (+ y dy)))
    (scale (factor)
	   (set! radius (* radius factor))))))

Obviously, the "class" macro should convert methods
to lambdas. For "move" that would be:

  (lambda(self dx dy)
    (let((x (vector-loc self (get-hash-table properties 'x)))
           (y (vector-loc self (get-hash-table properties 'y)))
           (radius (vector-ref self (get-hash-table properties 'radius))))
      (set! x (+ x dx))
      (set! y (+ y dy)))

Obviously, the hash table reference would have to be
evaluated during macro expand to provide the maximum
efficiency.
(If you have any other solutions to this problem, I'm open
to suggestions)

>> Besides I think that the names "hash-ref"
>> and "vector-ref" are confusing, since they
>> don't return references, but values (therefore
>> the names like "vector-get" or "hash-get" would
>> be more apropreate)
>>
>
> Never thought about that, but it sounds
> correct. vector-get and hash-get would
> probably be mroe apropreate names.

On the other hand the world of scheme seems
to have gotten used to such convention.

> Well, here's a relatively clean way, I think:
>
> (define-macro (location name)
>   (define new-val (gensym))
>   `(lambda (,new-val)
>       (set! ,name ,new-val)))
>
> (define old-set! set!)
>
> (define-macro (set! a b)
>  `(if (procedure? ,a)  ;; Needs a better check.
>       (,a ,b)
>       (old-set! ,a ,b)))
>
>
> guile> (define x 10)
> guile> (define y (location x))
> guile> (set! y 20)
> guile> x
> 20
> guile>

That's amazing! Whenever I think of something (and imagine
how difficult would it be to implement), you come up with a
solution taking only a few lines of code :)
(I only have to think how would it perform in practice)

Thanks
M.




  reply	other threads:[~2008-08-10 23:00 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <cmu-lmtpd-5833-1218211546-4@mail-imap1.uio.no>
2008-08-08 18:34 ` References/locations Kjetil S. Matheussen
2008-08-10 23:00   ` Maciek Godek [this message]
2008-08-15 10:14   ` References/locations Maciek Godek
2008-08-15 13:55     ` References/locations Clinton Ebadi
2008-08-18  8:09       ` References/locations Maciek Godek
2008-08-18 16:15         ` References/locations Clinton Ebadi
2008-08-08  1:50 References/locations Maciek Godek
2008-08-10  7:51 ` References/locations Stephen Compall
2008-08-15  9:35   ` References/locations Maciek Godek
     [not found]   ` <e2ceda030808141732r524fc0cehb8ae7c6007550ea6@mail.gmail.com>
     [not found]     ` <m2wsihltfv.fsf@member.fsf.org>
     [not found]       ` <e2ceda030808200441q52d9858bv63323055e0f6308a@mail.gmail.com>
2008-08-21  5:05         ` References/locations Stephen Compall

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e2ceda030808101600k2469efa7k56800fa8d480d3c@mail.gmail.com \
    --to=pstrychuj@gmail.com \
    --cc=guile-user@gnu.org \
    --cc=k.s.matheussen@notam02.no \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).