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: Linas Vepstas
Subject: Re: passing an alist to a procedure without making a copy?
Date: Sat, 18 Apr 2009 20:20:39 -0500

2009/4/18 Mark Polesky <address@hidden>:
>
> (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 via email to

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