unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Clinton Ebadi <clinton@unknownlamer.org>
To: "Maciek Godek" <pstrychuj@gmail.com>
Cc: guile-user@gnu.org
Subject: Re: set-pair!
Date: Wed, 20 Aug 2008 12:34:56 -0400	[thread overview]
Message-ID: <87od3nzlu7.fsf@unknownlamer.org> (raw)
In-Reply-To: <e2ceda030808200027m53bd490dq6e34a534915dc410@mail.gmail.com> (Maciek Godek's message of "Wed\, 20 Aug 2008 09\:27\:48 +0200")

"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




  reply	other threads:[~2008-08-20 16:34 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-20  7:27 set-pair! Maciek Godek
2008-08-20 16:34 ` Clinton Ebadi [this message]
2008-09-11 15:09   ` phase separation -- was set-pair! JonWilson
2008-09-11 15:12     ` Robby Findler

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=87od3nzlu7.fsf@unknownlamer.org \
    --to=clinton@unknownlamer.org \
    --cc=guile-user@gnu.org \
    --cc=pstrychuj@gmail.com \
    /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).