unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Nordlöw <per.nordlow@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Setting the nth element of a list
Date: Fri, 5 Jun 2009 06:10:06 -0700 (PDT)	[thread overview]
Message-ID: <10cee0d4-43ff-4d2b-9a9b-735c8f83ffd4@x6g2000vbg.googlegroups.com> (raw)
In-Reply-To: b775c362-a37c-4d38-bce1-8de6922acbd3@b1g2000vbc.googlegroups.com

On Jun 5, 3:06 pm, Nordlöw <per.nord...@gmail.com> wrote:
> On Jun 5, 4:25 am, Barry Margolin <bar...@alum.mit.edu> wrote:
>
>
>
> > In article
> > <8a7c4d36-195d-4517-827e-7fbeca7ae...@r34g2000vba.googlegroups.com>,
>
> >  Nordlöw <per.nord...@gmail.com> 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öw
>
> > (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)
>   "Set N:th element of LIST to X for side effects only."
>   (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)
>   "Set N:th element of ARRAY to X for side effects only."
>   (set (aref array n) x))
>
> Thanks in advance,
> Nordlöw

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öw


  reply	other threads:[~2009-06-05 13:10 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-04 21:08 Setting the nth element of a list Nordlöw
2009-06-04 22:06 ` Drew Adams
2009-06-04 22:35 ` Lennart Borgman
2009-06-05  2:25 ` Barry Margolin
2009-06-05 13:06   ` Nordlöw
2009-06-05 13:10     ` Nordlöw [this message]
2009-06-05 13:25       ` Pascal J. Bourguignon
2009-06-05 13:57         ` Nordlöw
2009-06-05 15:08           ` Pascal J. Bourguignon
2009-06-05 16:56           ` bigfaceworm
2009-06-06  6:01             ` Miles Bader
2009-06-05 20:51     ` Barry Margolin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=10cee0d4-43ff-4d2b-9a9b-735c8f83ffd4@x6g2000vbg.googlegroups.com \
    --to=per.nordlow@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).