unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Linas Vepstas <linasvepstas@gmail.com>
To: Mark Polesky <markpolesky@yahoo.com>
Cc: guile-user@gnu.org
Subject: Re: passing an alist to a procedure without making a copy?
Date: Sat, 18 Apr 2009 20:20:39 -0500	[thread overview]
Message-ID: <3ae3aa420904181820s22c372a6nf8d01b1c5f3d4728@mail.gmail.com> (raw)
In-Reply-To: <484811.24959.qm@web83405.mail.sp1.yahoo.com>

2009/4/18 Mark Polesky <markpolesky@yahoo.com>:
>
> (define my-alist
>  '((a . 1)
>    ))
>
> (set! my-alist (acons 'b 2 my-alist))
>
> my-alist ==> ((b . 2) (a . 1))
>
> (define (alist-prepend alist key value)
>  (set! alist (acons key value alist)))
>
> (alist-prepend my-alist 'c 3)
>
> my-alist ==> ((b . 2) (a . 1))
>
> ________________________________
>
> How can I get (alist-prepend) to operate
> on the original alist?

Create a pointer to it:

guile> (define p-list (list my-alist))
guile> p-list
(((b . 2) (a . 1)))
guile> (define (alist-prepend palist key value)
... (set-car! palist (acons key value (car palist))))
guile> (alist-prepend p-list  'c 3)
guile>  p-list
(((c . 3) (b . 2) (a . 1)))

However, this seems like an awkward way of doing
things; I'm thinking that you are bringing a
C/perl/python/java mindset to scheme, and that will
hobble you in general.  For starters, always think
"how can I solve this problem without using set!
(or set-car!, or any kind of set!)"

--linas




  reply	other threads:[~2009-04-19  1:20 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-04-18 22:57 passing an alist to a procedure without making a copy? Mark Polesky
2009-04-19  1:20 ` Linas Vepstas [this message]
2009-04-19  7:30   ` r6rsguy
2009-04-19 17:35 ` Clinton Ebadi
2009-04-20 23:36   ` Neil Jerram

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=3ae3aa420904181820s22c372a6nf8d01b1c5f3d4728@mail.gmail.com \
    --to=linasvepstas@gmail.com \
    --cc=guile-user@gnu.org \
    --cc=markpolesky@yahoo.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).