From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: =?ISO-8859-1?Q?Nordl=F6w?= Newsgroups: gmane.emacs.help Subject: Re: Setting the nth element of a list Date: Fri, 5 Jun 2009 06:57:57 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: <8a7c4d36-195d-4517-827e-7fbeca7ae03f@r34g2000vba.googlegroups.com> <10cee0d4-43ff-4d2b-9a9b-735c8f83ffd4@x6g2000vbg.googlegroups.com> <7ceityols1.fsf@pbourguignon.anevia.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1244212849 6389 80.91.229.12 (5 Jun 2009 14:40:49 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 5 Jun 2009 14:40:49 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Jun 05 16:40:46 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1MCab0-0006HP-Iw for geh-help-gnu-emacs@m.gmane.org; Fri, 05 Jun 2009 16:40:38 +0200 Original-Received: from localhost ([127.0.0.1]:38873 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MCaaz-00009T-SG for geh-help-gnu-emacs@m.gmane.org; Fri, 05 Jun 2009 10:40:37 -0400 Original-Path: news.stanford.edu!newsfeed.stanford.edu!postnews.google.com!r13g2000vbr.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 101 Original-NNTP-Posting-Host: 81.170.250.228 Original-X-Trace: posting.google.com 1244210278 27177 127.0.0.1 (5 Jun 2009 13:57:58 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Fri, 5 Jun 2009 13:57:58 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: r13g2000vbr.googlegroups.com; posting-host=81.170.250.228; posting-account=ytJKAgoAAAA1tg4ScoRszebXiIldA5vg User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1pre) Gecko/20090526 Ubuntu/9.04 (jaunty) Shiretoko/3.5pre, gzip(gfe), gzip(gfe) Original-Xref: news.stanford.edu gnu.emacs.help:169739 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:64969 Archived-At: On Jun 5, 3:25=A0pm, p...@informatimago.com (Pascal J. Bourguignon) wrote: > Nordl=F6w writes: > > On Jun 5, 3:06=A0pm, Nordl=F6w wrote: > >> On Jun 5, 4:25=A0am, Barry Margolin wrote: > > >> > In article > >> > <8a7c4d36-195d-4517-827e-7fbeca7ae...@r34g2000vba.googlegroups.com>, > > >> > =A0Nordl=F6w wrote: > >> > > This simple example changes the second element in the list x: > > >> > > (setq x '(a nil c)) > >> > > (setcar (cdr x 'b) > > >> > > How do I generalize this to a function that sets (changes) the nth > >> > > element of a list (for side effects only)? > > >> > > Thanks in advance, > >> > > Nordl=F6w > > >> > (require 'cl-macs) > >> > (setf (nth n x) 'b) > > >> Okey, here is my solution: > > >> (defun setnthcar (n list x) > >> =A0 "Set N:th element of LIST to X for side effects only." > >> =A0 (setcar (nthcdr n list) x)) > > >> Comments on that? > > It's awful, since there's is already a more standard and more general > mechanism to do that: (setf (nth n x) 'b) > > > > >> How do I accomplish the same with arrays? Using aref I guess. Here is > >> my mockup that doesn't yet do what I want: > > >> (defun setnthref (n array x) > >> =A0 "Set N:th element of ARRAY to X for side effects only." > >> =A0 (set (aref array n) x)) > > >> Thanks in advance, > >> Nordl=F6w > > > Okey I found a solution: > > > (defun setnthref (n array x) > > =A0 "Set N:th element of ARRAY to X for side effects only." > > =A0 (setf (aref array n) x)) > > ;; (equal (let ((l '[a b c])) (setnthref 1 l 'B) l) '[a B c]) > > > Is this the most efficient way? I see that setf() is quite > > complicated. > > Setf is a macro that is quite complicated, but it generates the best > code to properly update a place. > > (require 'cl) > (macroexpand '(setf (aref (aref (nth (incf a) list-of-arrays) (incf i)) n= ) 42)) > --> > (let* ((#1=3D--cl-var-- (aref (nth (incf a) list-of-arrays) (incf i))) > =A0 =A0 =A0 =A0(#2=3D--cl-var-- n)) > =A0 (aset #1# #2# 42)) > > There's nothing more efficient to set an array than aset. > Once you've found the array to set... > > So there's no need to define a setnthref for an array, since (setf > (aref a n) v) does the job better. > > There's also no need to define a setnthcar for a list, since (setf > (nth l n) v) does the job better. > > And if you don't know whether you have a list or a vector, there's no > need to define anything, since (setf (elt sequence n) v) works better. > > (let (x) > =A0 =A0(setf (elt (setf x (funcall (if (oddp (random 2)) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(f= unction list) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(f= unction vector)) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A01 2 3)) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A01) > =A0 =A0 =A0 =A0 =A00) > =A0 =A0x) > --> [1 0 3] or (1 0 3) > > -- > __Pascal Bourguignon__ I really believe such a function increases the readability of the code that uses it, don't you? What if I define it as a macro (defmacro) or inline (defsubst)? Doesn't that give the best of both worlds+ /Nordl=F6w