unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* passing an alist to a procedure without making a copy?
@ 2009-04-18 22:57 Mark Polesky
  2009-04-19  1:20 ` Linas Vepstas
  2009-04-19 17:35 ` Clinton Ebadi
  0 siblings, 2 replies; 5+ messages in thread
From: Mark Polesky @ 2009-04-18 22:57 UTC (permalink / raw)
  To: guile-user


(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?

Thanks,
Mark



      




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

* Re: passing an alist to a procedure without making a copy?
  2009-04-18 22:57 passing an alist to a procedure without making a copy? Mark Polesky
@ 2009-04-19  1:20 ` Linas Vepstas
  2009-04-19  7:30   ` r6rsguy
  2009-04-19 17:35 ` Clinton Ebadi
  1 sibling, 1 reply; 5+ messages in thread
From: Linas Vepstas @ 2009-04-19  1:20 UTC (permalink / raw)
  To: Mark Polesky; +Cc: guile-user

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




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

* Re: passing an alist to a procedure without making a copy?
  2009-04-19  1:20 ` Linas Vepstas
@ 2009-04-19  7:30   ` r6rsguy
  0 siblings, 0 replies; 5+ messages in thread
From: r6rsguy @ 2009-04-19  7:30 UTC (permalink / raw)
  To: linasvepstas; +Cc: markpolesky, guile-user

> From: Linas Vepstas <linasvepstas@gmail.com>
> 
> 2009/4/18 Mark Polesky <markpolesky@yahoo.com>:
> > Subject: passing an alist to a procedure without making a copy?
> >
> > (define my-alist '((a . 1)) )
> >
> > (set! my-alist (acons 'b 2 my-alist))
> >
> > (define (alist-prepend alist key value)
> >  (set! alist (acons key value alist)))
> >
> > (alist-prepend my-alist 'c 3)
> > ________________________________
> >
> > 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
> 

The answer that Linas gives is totally correct.

But I notice the subject line of the original
question has to do with avoiding a copy, which
makes me think that the awkward desire arises
from a misunderstanding of when a copy happens.

This makes only one list of numbers and never
copies it:

(define (upto N)
  (define (consdown k ls)
     (if (zero? k)
         ls
         (consdown (- k 1) (cons k ls)) ))
  (consdown N '()) )

(define a (upto 5))
(define b a)
(define c b)
(list a b c)

==> ((1 2 3 4 5) (1 2 3 4 5) (1 2 3 4 5))

The numbers upto 5 will be found only once
in memory, even though they have each been
printed three times, and are part of the
value of three variables.  The recursive
definition of (upto N) will be run with
no extra space no matter how big N is.
(That is, only a small fixed amount of
space beyond that required to store one
copy of the result.)

   -- Keith




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

* Re: passing an alist to a procedure without making a copy?
  2009-04-18 22:57 passing an alist to a procedure without making a copy? Mark Polesky
  2009-04-19  1:20 ` Linas Vepstas
@ 2009-04-19 17:35 ` Clinton Ebadi
  2009-04-20 23:36   ` Neil Jerram
  1 sibling, 1 reply; 5+ messages in thread
From: Clinton Ebadi @ 2009-04-19 17:35 UTC (permalink / raw)
  To: Mark Polesky; +Cc: guile-user

Mark Polesky <markpolesky@yahoo.com> writes:

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

There is no real copy involved; `my-alist' points to a non-immediate
value and so the only reason your `set!' does not work is because it is
reassigning the binding `alist' within `alist-prepend'.

This smells a bit like homework, however, and so how to achieve the
result you desire is left as an exercise (a very simple one at that--if
it's still not obvious and this isn't homework naturally an answer will
be provided, but just in case...). Ask yourself: *why* do you want to
destructively modify the original list? Do you even have to modify the
value of `my-alist' to achieve what you wish?

-- 
unknownlamer: online dating is dumb in general 
emacsen: computer dating rocks. I love computer
emacsen: I mean I really LOVE computers ;)




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

* Re: passing an alist to a procedure without making a copy?
  2009-04-19 17:35 ` Clinton Ebadi
@ 2009-04-20 23:36   ` Neil Jerram
  0 siblings, 0 replies; 5+ messages in thread
From: Neil Jerram @ 2009-04-20 23:36 UTC (permalink / raw)
  To: Clinton Ebadi; +Cc: Mark Polesky, guile-user

Clinton Ebadi <clinton@unknownlamer.org> writes:

> This smells a bit like homework, [...]

I personally don't mind if it's homework or not, and in case there are
students out there wondering whether to ask questions on this list:
please do!  (Assuming that there is some connection with Guile/Scheme,
of course.)

(I don't mean by this to criticize what Clinton wrote...  Education is
complex; sometimes it works well to give hints, sometimes to give a
full answer, and different people have different approaches.)

     Neil




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

end of thread, other threads:[~2009-04-20 23:36 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-04-18 22:57 passing an alist to a procedure without making a copy? Mark Polesky
2009-04-19  1:20 ` Linas Vepstas
2009-04-19  7:30   ` r6rsguy
2009-04-19 17:35 ` Clinton Ebadi
2009-04-20 23:36   ` Neil Jerram

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