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:10:06 -0700 (PDT) Organization: http://groups.google.com Message-ID: <10cee0d4-43ff-4d2b-9a9b-735c8f83ffd4@x6g2000vbg.googlegroups.com> References: <8a7c4d36-195d-4517-827e-7fbeca7ae03f@r34g2000vba.googlegroups.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 1244209638 27536 80.91.229.12 (5 Jun 2009 13:47:18 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 5 Jun 2009 13:47:18 +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 15:47:16 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 1MCZlL-0003U0-R0 for geh-help-gnu-emacs@m.gmane.org; Fri, 05 Jun 2009 15:47:16 +0200 Original-Received: from localhost ([127.0.0.1]:59351 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MCZlL-0000sl-6P for geh-help-gnu-emacs@m.gmane.org; Fri, 05 Jun 2009 09:47:15 -0400 Original-Path: news.stanford.edu!newsfeed.stanford.edu!postnews.google.com!x6g2000vbg.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 58 Original-NNTP-Posting-Host: 81.170.250.228 Original-X-Trace: posting.google.com 1244207406 22032 127.0.0.1 (5 Jun 2009 13:10:06 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Fri, 5 Jun 2009 13:10:06 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: x6g2000vbg.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:169734 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:64967 Archived-At: 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) > > > -- > > Barry Margolin, bar...@alum.mit.edu > > Arlington, MA > > *** PLEASE post questions in newsgroups, not directly to me *** > > *** PLEASE don't copy me on replies, I'll read them in the group *** > > 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? > > 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) "Set N:th element of ARRAY to X for side effects only." (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. /Nordl=F6w