unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* set-pair!
@ 2008-08-20  7:27 Maciek Godek
  2008-08-20 16:34 ` set-pair! Clinton Ebadi
  0 siblings, 1 reply; 4+ messages in thread
From: Maciek Godek @ 2008-08-20  7:27 UTC (permalink / raw)
  To: guile-user

Hi,
I've been trying to write a function or macro that
would work like this:

(let ((a 0)(b 0))
  (set-pair! '(a . b) '(1 . 2))
  (cons a b)) ; => (1 . 2)

I eventually wrote:

(define (set-pair! pair values)
  (let ( (e (the-environment)) (a (car pair)) (d (cdr pair)) )
     (local-eval
        `(begin
             (set! ,a ,(car values))
             (set! ,d ,(cdr values)))
         e)))

but it seems to work only for symbols defined in
global scope. Does anyone know how to implement
this form properly?

Thanks in advance
Maciek

PS I love you all :)




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: set-pair!
  2008-08-20  7:27 set-pair! Maciek Godek
@ 2008-08-20 16:34 ` Clinton Ebadi
  2008-09-11 15:09   ` phase separation -- was set-pair! JonWilson
  0 siblings, 1 reply; 4+ messages in thread
From: Clinton Ebadi @ 2008-08-20 16:34 UTC (permalink / raw)
  To: Maciek Godek; +Cc: guile-user

"Maciek Godek" <pstrychuj@gmail.com> writes:

> Hi,
> I've been trying to write a function or macro that
> would work like this:
>
> (let ((a 0)(b 0))
>   (set-pair! '(a . b) '(1 . 2))
>   (cons a b)) ; => (1 . 2)
>
> I eventually wrote:
>
> (define (set-pair! pair values)
>   (let ( (e (the-environment)) (a (car pair)) (d (cdr pair)) )
>      (local-eval
>         `(begin
>              (set! ,a ,(car values))
>              (set! ,d ,(cdr values)))
>          e)))
>
> but it seems to work only for symbols defined in
> global scope. Does anyone know how to implement
> this form properly?

You should use a macro instead... something like:

(define-macro (mset! . forms)
  `(begin
     ,@(let mset-loop ((unprocessed-forms forms)
		       (processed-forms '()))
	(cond ((null? unprocessed-forms)
	       (reverse! processed-forms))
	      (else (mset-loop (cddr unprocessed-forms)
			       (cons `(set! ,(car unprocessed-forms)
					    ,(cadr unprocessed-forms))
				     processed-forms)))))))

(let ((a 0)
      (b 0))
  (mset! a 1 b 2) ; => (begin (set! a 1) (set! b 2))
  (cons a b)) ; => (1 . 2)

The syntax for mset! is the same as Common Lisp's setf[0]. You can
easily adjust it to take the form (mset! (variables) (values)) if you
really wanted. The macro version will interact properly with the local
environment, and still work whenever local-eval is removed.

Whenever you think you need local-eval you probably need a
macro. Destructively modifying environments at runtime is heavily
discouraged, and more or less deprecated (Guile acros and macros are
deprecated in favor of mmacros for a reason--runtime modification of
lexical environments is incompatible with a properly phase-separated
compiler).

[0] http://www.lispworks.com/documentation/HyperSpec/Body/m_setf_.htm

-- 
Danielle: well road signs were pissing me off
Danielle: I took one of them out, but the other haven't followed as
          planned




^ permalink raw reply	[flat|nested] 4+ messages in thread

* phase separation -- was Re: set-pair!
  2008-08-20 16:34 ` set-pair! Clinton Ebadi
@ 2008-09-11 15:09   ` JonWilson
  2008-09-11 15:12     ` Robby Findler
  0 siblings, 1 reply; 4+ messages in thread
From: JonWilson @ 2008-09-11 15:09 UTC (permalink / raw)
  To: guile-user

Hi Clinton,

Clinton Ebadi wrote:
> runtime modification of
> lexical environments is incompatible with a properly phase-separated
> compiler).

I hear a lot of mention of phase separation, usually as something
desirable.  However, I've never seen a thorough explanation of why phase
separation is particularly good thing.  (Nor a thorough explanation of
exactly what it is, and google hasn't turned up anything useful so far.)
 Would you mind giving me some pointers to reading material (or, if
you've got nothing which is actually important to do, writing up an
explanation yourself)?
Regards,
Jon




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: phase separation -- was Re: set-pair!
  2008-09-11 15:09   ` phase separation -- was set-pair! JonWilson
@ 2008-09-11 15:12     ` Robby Findler
  0 siblings, 0 replies; 4+ messages in thread
From: Robby Findler @ 2008-09-11 15:12 UTC (permalink / raw)
  To: JonWilson; +Cc: guile-user

You might try reading this paper:

http://www.cs.utah.edu/plt/publications/macromod.pdf

Robby

On Thu, Sep 11, 2008 at 10:09 AM, JonWilson <jsw@wilsonjc.us> wrote:
> Hi Clinton,
>
> Clinton Ebadi wrote:
>> runtime modification of
>> lexical environments is incompatible with a properly phase-separated
>> compiler).
>
> I hear a lot of mention of phase separation, usually as something
> desirable.  However, I've never seen a thorough explanation of why phase
> separation is particularly good thing.  (Nor a thorough explanation of
> exactly what it is, and google hasn't turned up anything useful so far.)
>  Would you mind giving me some pointers to reading material (or, if
> you've got nothing which is actually important to do, writing up an
> explanation yourself)?
> Regards,
> Jon
>
>
>
>




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-09-11 15:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-08-20  7:27 set-pair! Maciek Godek
2008-08-20 16:34 ` set-pair! Clinton Ebadi
2008-09-11 15:09   ` phase separation -- was set-pair! JonWilson
2008-09-11 15:12     ` Robby Findler

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).