help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Setting the nth element of a list


From: Nordlöw
Subject: Re: Setting the nth element of a list
Date: Fri, 5 Jun 2009 06:57:57 -0700 (PDT)
User-agent: G2/1.0

On Jun 5, 3:25 pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> Nordlöw <per.nord...@gmail.com> writes:
> > 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)
>
> >> 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?
>
> 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)
> >>   "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.
>
> 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=--cl-var-- (aref (nth (incf a) list-of-arrays) (incf i)))
>        (#2=--cl-var-- n))
>   (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)
>    (setf (elt (setf x (funcall (if (oddp (random 2))
>                                    (function list)
>                                    (function vector))
>                                1 2 3))
>                1)
>          0)
>    x)
> --> [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öw


reply via email to

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