guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: passing an alist to a procedure without making a copy?


From: Clinton Ebadi
Subject: Re: passing an alist to a procedure without making a copy?
Date: Sun, 19 Apr 2009 13:35:29 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.92 (gnu/linux)

Mark Polesky <address@hidden> 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 ;)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]