From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Kjetil S. Matheussen" Newsgroups: gmane.lisp.guile.user Subject: Re: References/locations Date: Fri, 08 Aug 2008 20:34:27 +0200 (CEST) Message-ID: References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7BIT X-Trace: ger.gmane.org 1218220505 508 80.91.229.12 (8 Aug 2008 18:35:05 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 8 Aug 2008 18:35:05 +0000 (UTC) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Fri Aug 08 20:35:56 2008 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1KRWoe-0004ji-9A for guile-user@m.gmane.org; Fri, 08 Aug 2008 20:35:56 +0200 Original-Received: from localhost ([127.0.0.1]:40720 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KRWni-0000eG-Dj for guile-user@m.gmane.org; Fri, 08 Aug 2008 14:34:58 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KRWnW-0000Wc-KG for guile-user@gnu.org; Fri, 08 Aug 2008 14:34:48 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KRWnN-0000N5-49 for guile-user@gnu.org; Fri, 08 Aug 2008 14:34:38 -0400 Original-Received: from [199.232.76.173] (port=46806 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KRWnM-0000MF-8h for guile-user@gnu.org; Fri, 08 Aug 2008 14:34:36 -0400 Original-Received: from smtp.getmail.no ([84.208.20.33]:53060) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1KRWnK-0004Fq-Ph for guile-user@gnu.org; Fri, 08 Aug 2008 14:34:35 -0400 Original-Received: from pmxchannel-daemon.no-osl-m323-srv-009-z2.isp.get.no by no-osl-m323-srv-009-z2.isp.get.no (Sun Java System Messaging Server 6.2-7.05 (built Sep 5 2006)) id <0K5A0080LOXKVP00@no-osl-m323-srv-009-z2.isp.get.no> for guile-user@gnu.org; Fri, 08 Aug 2008 20:34:32 +0200 (CEST) Original-Received: from smtp.getmail.no ([10.5.16.1]) by no-osl-m323-srv-009-z2.isp.get.no (Sun Java System Messaging Server 6.2-7.05 (built Sep 5 2006)) with ESMTP id <0K5A00GYZOXGW060@no-osl-m323-srv-009-z2.isp.get.no> for guile-user@gnu.org; Fri, 08 Aug 2008 20:34:28 +0200 (CEST) Original-Received: from cm-84.215.155.160.getinternet.no ([84.215.155.160]) by no-osl-m323-srv-004-z1.isp.get.no (Sun Java System Messaging Server 6.2-7.05 (built Sep 5 2006)) with ESMTP id <0K5A00CZ3OXG2RP2@no-osl-m323-srv-004-z1.isp.get.no> for guile-user@gnu.org; Fri, 08 Aug 2008 20:34:28 +0200 (CEST) In-reply-to: X-X-Sender: kjetil@ttleush X-detected-kernel: by monty-python.gnu.org: Solaris 10 (beta) X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:6730 Archived-At: "Maciek Godek": > Subject: References/locations > To: guile-user@gnu.org > Message-ID: > > Content-Type: text/plain; charset=ISO-8859-1 > > Hi, > it's me again, asking silly questions. Not at all, your questions are very good. :-) > This time I would like to know if there's > a simple way to store a reference to > a variable in another variable -- say, > an element of vector or hash table. > > I imagine this could look like this: > (define v #(1 2 3)) > (define v1 (vector-location v 1)) > v1 > : 2 > (set! v1 10) > v > : #(1 10 3) > > I've tried to do it using a "procedure with > setter", but the problem is that set! doesn't > evaluate its first argument (as long as it's a > symbol), so I'd have to wrap everything > up in macros to obtain: > (set! (vector-location v 1) 10) > > Besides I think that the names "hash-ref" > and "vector-ref" are confusing, since they > don't return references, but values (therefore > the names like "vector-get" or "hash-get" would > be more apropreate) > Never thought about that, but it sounds correct. vector-get and hash-get would probably be mroe apropreate names. > I also wonder if there's any point for allowing > locations to any sorts of variables (similar to > pointers in C or pointers to C++), that is, > (define x 10) > (define y (location x)) > (set! y 20) > x > : 20 > Well, here's a relatively clean way, I think: (define-macro (location name) (define new-val (gensym)) `(lambda (,new-val) (set! ,name ,new-val))) (define old-set! set!) (define-macro (set! a b) `(if (procedure? ,a) ;; Needs a better check. (,a ,b) (old-set! ,a ,b))) guile> (define x 10) guile> (define y (location x)) guile> (set! y 20) guile> x 20 guile>