From: Young-Jin Lee
Subject: Lisp novice's question
Date: 
Message-ID: <edBT5.3686$4h2.64417@vixen.cso.uiuc.edu>
Hi, I'm looking for a help in manipulating lisp data.
I want to remove nth element in the list. For example, I have a (1 2 2 3 4)
and I'd like to remove the fourth element which is '3'. If the length of the
list is fixed, it would be a trivial, but the length of a list varies in my
case.
I thought of traversing with loop, but I think there must be an easy and
efficient way of doing this.
Thanks in advance. I'm looking forward to getting LISP guru's answer.

YJ

From: Lieven Marchand
Subject: Re: Lisp novice's question
Date: 
Message-ID: <m3vgtddqh1.fsf@localhost.localdomain>
"Young-Jin Lee" <······@uiuc.edu> writes:

> Hi, I'm looking for a help in manipulating lisp data.
> I want to remove nth element in the list. For example, I have a (1 2 2 3 4)
> and I'd like to remove the fourth element which is '3'. If the length of the
> list is fixed, it would be a trivial, but the length of a list varies in my
> case.
> I thought of traversing with loop, but I think there must be an easy and
> efficient way of doing this.

(setf (nthcdr 3 *foo*) (nthcdr 4 *foo*))

(setf *foo* (delete (nth 3 *foo*) *foo* :start 3))

etc.

-- 
Lieven Marchand <···@bewoner.dma.be>
Lambda calculus - Call us a mad club
From: Kent M Pitman
Subject: Re: Lisp novice's question
Date: 
Message-ID: <sfwzoip57g3.fsf@world.std.com>
"Young-Jin Lee" <······@uiuc.edu> writes:

> Hi, I'm looking for a help in manipulating lisp data.

I'm not answering directly since this seems like it might be a class 
assignment.  I hope the info I provide is helpful in any case.  When asking
easy questions like this about list manipulation in particular, you should
identify whether it's for a class or not, since you'll get more direct answers
from people if it's not for a class.

> I want to remove nth element in the list. For example, I have a (1 2 2 3 4)
> and I'd like to remove the fourth element which is '3'. If the length of the
> list is fixed, it would be a trivial, but the length of a list varies in my
> case.

(Your problem is complicated throughout by the fact that "computers count from
0 and people count from 1".  So the fourth element of this list is what
Lisp calls element 3 in all cases except the english-named functions FIRST,
SECOND, etc. which all give you NTH 0, NTH 1, etc. respectively.  You have
used human terms but you need a translation phase to 0-based indexes.)

- - - -

There isn't a specific system function to delete the nth, but there are 
functions to find an nth tail.  You could, for example, use NTHCDR to find
the tail that holds cell in question and then remove it.  In this case,
you have the list

 (1 . (2 . (2 . (3 . (4 . ())))))
                ^    ^
and you want to remove the first indicated cons cell, replacing it with
the second.  If you can reduce the problem to having this in your hand:

 temp = (2 . (3 . (4 . ())))

then you could just do (setf (cdr temp) (cddr temp)), so you need to have
something which finds that cons cell, tests that it's not NIL (i.e., that
the list isn't too short for this operation and you're not going to try
to clobber the CDR of NIL instead of CDR of some cons).

- - - -

A different way of doing this problem is to use SUBSEQ.  Here, though,
the problem is one of efficiency--not algorithmic efficiency, since that
is unaffected by an extra call to LENGTH, but practical efficiency, since
you want to call SUBSEQ with (MIN 3 (LENGTH LIST)) in at least some
implementations.  In a sense, ignoring the boundary case where the list
is too short (APPEND (SUBSEQ LIST 0 3) (SUBSEQ LIST 4)) will do what
you want, but since APPEND will potentially copy the result and so will
SUBSEQ, you'll be doing more consing than you want.  Better would be to 
write a FIRSTN function, as in
 (DEFUN FIRSTN (LIST N)
   (LOOP FOR I BELOW N
         FOR ELT IN LIST
         COLLECT ELT))
and then use (NCONC (FIRSTN LIST 3) (SUBSEQ LIST 4))
but even this makes two unnecessary passes over the list, one for the NCONC
(which is a destructive append) and one for the SUBSEQ.  

- - - - - 

The best you're likely to do if you want something totally optimal is
probably to write the loop yourself, accumulating the elements as you
go and calling NRECONC at the end.  That should be precisely optimal.
I'll leave that as an exercise since it's probably closest to what an
instructor would want and you haven't said if this is homework.  (In
all my other examples here, I've used mostly operators instructors
tend to hate but that practical programmers use all the time.)

> I thought of traversing with loop, but I think there must be an easy and
> efficient way of doing this.

Underlying whatever you do, there will be a loop.  It's simply a question
of whether you write it or whether the system provides a function that
hides it.  Your writing a loop is fine.

> Thanks in advance. I'm looking forward to getting LISP guru's answer.

We don't have titles here, so I recommend avoiding this kind of terminology
in the future.  If I thought replying to this meant I had to be a guru,
not having graduated myself from any guru-certifying agency, I would not
have replied.  I suppose everyone has their own heroes and villains here, 
but if you're looking for wisdom, here's mine:  focus on getting a good 
answer that seems plausible not because of where it came from but because
it's explained clearly enough that you understand it and can test its value
for yourself.
From: Joe Marshall
Subject: Re: Lisp novice's question
Date: 
Message-ID: <wvdpxoe9.fsf@content-integrity.com>
Kent M Pitman <······@world.std.com> writes:

> "Young-Jin Lee" <······@uiuc.edu> writes:
> >
> > Thanks in advance. I'm looking forward to getting LISP guru's answer.
> 
> We don't have titles here, so I recommend avoiding this kind of terminology
> in the future.  If I thought replying to this meant I had to be a guru,
> not having graduated myself from any guru-certifying agency, I would not
> have replied.  I suppose everyone has their own heroes and villains here, 
> but if you're looking for wisdom, here's mine:  focus on getting a good 
> answer that seems plausible not because of where it came from but because
> it's explained clearly enough that you understand it and can test its value
> for yourself.

That being generally a generally good algorithm for evaluating advice
of unknown provenance, it is clear that some people in this group
offer far better advice than others (guru certification or lack
thereof notwithstanding).

In addition to the heroes and villians, there are clueless idiots.
Both my heroes and villians usually have very well-considered opinions
(that I may or may not agree with), but clueless idiots will spout
`plausible sounding nonsense' that can lead newbies astray.

The biggest problem, however, is partitioning the usual posters into
these three groups.



-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----
From: Thomas A. Russ
Subject: Re: Lisp novice's question
Date: 
Message-ID: <ymihf4tz3vt.fsf@sevak.isi.edu>
Kent M Pitman <······@world.std.com> writes:
> > Thanks in advance. I'm looking forward to getting LISP guru's answer.
> 
> We don't have titles here, so I recommend avoiding this kind of terminology
> in the future.  If I thought replying to this meant I had to be a guru,
> not having graduated myself from any guru-certifying agency, I would not
> have replied.

Notwithstanding that this suggestion conflicts directly with Kent's
philosophy on the issue:

I move that the readership of c.l.l annoint Kent with the title of
"Official Lisp Guru" by acclamation.

:)
-- 
Thomas A. Russ,  USC/Information Sciences Institute          ···@isi.edu    
From: Joe Marshall
Subject: Re: Lisp novice's question
Date: 
Message-ID: <zoilw584.fsf@content-integrity.com>
···@sevak.isi.edu (Thomas A. Russ) writes:

> Kent M Pitman <······@world.std.com> writes:
> > > Thanks in advance. I'm looking forward to getting LISP guru's answer.
> > 
> > We don't have titles here, so I recommend avoiding this kind of terminology
> > in the future.  If I thought replying to this meant I had to be a guru,
> > not having graduated myself from any guru-certifying agency, I would not
> > have replied.
> 
> Notwithstanding that this suggestion conflicts directly with Kent's
> philosophy on the issue:
> 
> I move that the readership of c.l.l annoint Kent with the title of
> "Official Lisp Guru" by acclamation.

Before we annoint him, however, we ought to ask him if he wishes to be
so annointed.  Being certified as a guru by a usenet community is not
necessarily an honor.


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----
From: Kent M Pitman
Subject: Personal credentials [was Re: Lisp novice's question]
Date: 
Message-ID: <sfwsnodyu4x.fsf_-_@world.std.com>
Joe Marshall <···@content-integrity.com> writes:

> ···@sevak.isi.edu (Thomas A. Russ) writes:
> 
> > I move that the readership of c.l.l annoint Kent with the title of
> > "Official Lisp Guru" by acclamation.
> 
> Before we annoint him, however, we ought to ask him if he wishes to be
> so annointed.  Being certified as a guru by a usenet community is not
> necessarily an honor.

Joe is right.  I wasn't fishing for people to reassure me that I was
ok.  I was really trying to deflect a newcomer from thinking that this
community buys into that very questionable "guru" thing.  In many cases,
I think guruhood reflects the holding hostage of a community by not 
releasing the keys to "obvious" and "generally useful" capabilities to
those who need it.  Things are given obscure names with vowels omitted
to make it hard to find them in the documentation, etc. in such a way
that a person can't tell if they are beholden to or victim of their gurus.

In my early days Lisping, I used to think I was oh-so-cool because *I* knew
things like that ERRSET didn't really work the way people expected it to
(it was sorta like IGNORE-ERRORS) because it had a friend variable that almost
no one knew about called, not coincidentally, ERRSET.  And you had to 
sometimes bind that variable to get the effect you expected.  *I* knew 
this and I was cool.  I figured if I amassed lots of stupid trivia like 
this, people would revere me.  But at some point early on I had the 
realization that this was nonsense and that anyone who really wanted to do
something useful for people wouldn't amass trivia like that and then charge
for it, but would figure out how to fix the damned language so that operators
did what they were supposed to and people's naive intuitions worked as much
as was reasonable, or at least switches were provided in obvious places with
obvious names to allow mere mortals to get control and to understand and
safely exercise that control.

As to villains and heroes, I'm aware that some here give greater
weight to my remarks than to some others; and then again, some don't.
I've learned from my side hobby of creative fiction that it's more
important to have an audience that's happy with you than to seek the
notion of "best author" or even "good enough" author.  And there are
others here who far exceed my skills in certain areas and are
routinely not given the time of day.

This all relates to my paper on EQUAL, btw.
 http://world.std.com/~pitman/PS/EQUAL.html 
Equality for one purpose not being equality for another purpose.
Equality operators are not aligned on a one-dimensional line with ones
to the left implying ones to the right.  They are arranged in free
space where equality only means anything when applied to a purpose.
And inequality follows suit.  And so, too, the bestowing of honor and
shame.  (To abuse my favorite (if somewhat lame duck) topic, the fact
of Clinton's relationship with Monica really says nothing about his
suitability to aid in middle east peace negotiations.)  [I feel like
Clinton has been shortchanged.  He is almost to philosophy as Kevin
Bacon is to movies.  Always a lesson in there somewhere.  I think it's
because he pushes things so close to the line all the time and
everything he does is a lesson in how the same thing can be perceived
so radically differently by different people.]

Mostly I like the free spirited open community we have.  I do
sometimes lament people not being able to tell which of us who opine
have "been there and done that".  And I've gotten particularly snippy
a few times with students because they are so independent these days
that they miss the value that is in front of them if they would just
listen instead of spouting off.  Perhaps sometimes we do need
some sort of reputation maintenance system and/or credential system,
but I'd hope if we had it it would be fine-tuned enough to work by
skill and not by general good-person-ness, since I think we each have
strengths and weaknesses and a proper view of us would say that we
complement each other.  There are things I know better than others
here, but there are things others know better than me.  The cool thing
about a good community is feeling comfortable asking for help and
knowing there are those who will fill in your gaps productively.

In the CS industry generally I've wondered if there should be credentials.
Sometimes I wish I had them.  But my guess is that if there were a single
master credential test, I'd fail it.  Some might think that as it should
be, but I'd hope a few others wouldn't.  Part of me thinks maybe a pedigree
system (a kind of master/apprentice thing with people vouching for others
they've taught) would be kind of cool.  But mostly I guess I'm glad that
with as much "religion substituting for science" as there is around CS,
no one has tried to make any of this too firm.

My business card says "Information Architect".  I like it because it says
what I do in the soup of things, without trying to position me at a vertical
position on a ladder.  Figuring out our respective roles and being "suitable
for the purpose" seems more important than seeking some one-size-fits-all
moniker of guru.  The very nature of it points out its lack of practical
utility, other than to make mystic that which should be perspicuous.
From: Joe Marshall
Subject: Re: Personal credentials [was Re: Lisp novice's question]
Date: 
Message-ID: <8zq5vy7i.fsf@content-integrity.com>
Kent M Pitman <······@world.std.com> writes:

> And there are others here who far exceed my skills in certain areas
> and are routinely not given the time of day.

Most likely because they are sure that they already know everything
about the current time.


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----
From: Rob Warnock
Subject: Re: Lisp novice's question
Date: 
Message-ID: <8vvavj$2vgl2$1@fido.engr.sgi.com>
Joe Marshall  <···@content-integrity.com> wrote:
+---------------
| ···@sevak.isi.edu (Thomas A. Russ) writes:
| > I move that the readership of c.l.l annoint Kent with the title of
| > "Official Lisp Guru" by acclamation.
| 
| Before we annoint him, however, we ought to ask him if he wishes to be
| so annointed.  Being certified as a guru by a usenet community is not
| necessarily an honor.
+---------------

Yeah, especially if the "oily substance" [see nearest dictionary]
you anoint him with is particularly obnoxious...   ;-}


-Rob

-----
Rob Warnock, 31-2-510		····@sgi.com
Network Engineering		http://reality.sgi.com/rpw3/
Silicon Graphics, Inc.		Phone: 650-933-1673
1600 Amphitheatre Pkwy.		PP-ASEL-IA
Mountain View, CA  94043
From: Erik Naggum
Subject: Re: Lisp novice's question
Date: 
Message-ID: <3184410861082878@naggum.net>
* Thomas A. Russ
| I move that the readership of c.l.l annoint Kent with the title of
| "Official Lisp Guru" by acclamation.

  Gurus can't be wrong.  If you take away somebody's ability to make
  mistakes, you kill their creativity and punish them for their past
  successes in ways that you should hope _never_ to experience.

#:Erik
-- 
  Solution to U.S. Presidential Election Crisis 2000:
    Let Texas secede from the Union and elect George W. Bush their
    very first President.  All parties, states would rejoice.
From: ········@hex.net
Subject: Re: Lisp novice's question
Date: 
Message-ID: <wklmu37g6n.fsf@441715.i-did-not-set--mail-host-address--so-shoot-me>
>>>>> "Erik" == Erik Naggum <····@naggum.net> writes:

    Erik> * Thomas A. Russ | I move that the readership of c.l.l
    Erik> annoint Kent with the title of | "Official Lisp Guru" by
    Erik> acclamation.

    Erik>   Gurus can't be wrong.  If you take away somebody's ability
    Erik> to make mistakes, you kill their creativity and punish them
    Erik> for their past successes in ways that you should hope
    Erik> _never_ to experience.

Ah, well.  I'll demand continuous recounts of ballots until the
anointing takes place with the title "Unofficial Lisp Guru."
-- 
(concatenate 'string "cbbrowne" ·@hex.net")
<http://www.ntlug.org/~cbbrowne/>
"I once witnessed  a long-winded, month-long flamewar over  the use of
mice vs. trackballs...It was very silly." -- Matt Welsh
From: Marco Antoniotti
Subject: Re: Lisp novice's question
Date: 
Message-ID: <y6cbsv4vtkc.fsf@octagon.mrl.nyu.edu>
"Young-Jin Lee" <······@uiuc.edu> writes:

> Hi, I'm looking for a help in manipulating lisp data.
> I want to remove nth element in the list. For example, I have a (1 2 2 3 4)
> and I'd like to remove the fourth element which is '3'. If the length of the
> list is fixed, it would be a trivial, but the length of a list varies in my
> case.
> I thought of traversing with loop, but I think there must be an easy and
> efficient way of doing this.
> Thanks in advance. I'm looking forward to getting LISP guru's answer.

The answer is RTFM!

	(defvar the-list '(1 2 2 3 4))
	(delete (nth 3 the-list) the-list)  ; Count from 0.

For extra credit, tell us what is the time this algorithm takes and
what could go wrong with it :)

Cheers

-- 
Marco Antoniotti =============================================================
NYU Bioinformatics Group			 tel. +1 - 212 - 998 3488
719 Broadway 12th Floor                          fax  +1 - 212 - 995 4122
New York, NY 10003, USA				 http://galt.mrl.nyu.edu/valis
             Like DNA, such a language [Lisp] does not go out of style.
			      Paul Graham, ANSI Common Lisp
From: Rainer Joswig
Subject: Re: Lisp novice's question
Date: 
Message-ID: <joswig-D1A689.18474525112000@news.is-europe.net>
In article <···············@octagon.mrl.nyu.edu>, Marco Antoniotti 
<·······@cs.nyu.edu> wrote:

> "Young-Jin Lee" <······@uiuc.edu> writes:
> 
> > Hi, I'm looking for a help in manipulating lisp data.
> > I want to remove nth element in the list. For example, I have a (1 2 2 3 4)
> > and I'd like to remove the fourth element which is '3'. If the length of the
> > list is fixed, it would be a trivial, but the length of a list varies in my
> > case.
> > I thought of traversing with loop, but I think there must be an easy and
> > efficient way of doing this.
> > Thanks in advance. I'm looking forward to getting LISP guru's answer.
> 
> The answer is RTFM!
> 
> 	(defvar the-list '(1 2 2 3 4))
> 	(delete (nth 3 the-list) the-list)  ; Count from 0.

How about deleting just the nth element. ;-)

-- 
Rainer Joswig, Hamburg, Germany
Email: ·············@corporate-world.lisp.de
Web: http://corporate-world.lisp.de/
From: Kent M Pitman
Subject: Re: Lisp novice's question
Date: 
Message-ID: <sfwvgtblr22.fsf@world.std.com>
Rainer Joswig <······@corporate-world.lisp.de> writes:

> > The answer is RTFM!
> > 
> > 	(defvar the-list '(1 2 2 3 4))
> > 	(delete (nth 3 the-list) the-list)  ; Count from 0.
> 
> How about deleting just the nth element. ;-)

Well, there ARE two meanings of "delete the nth element".  That is,
"delete from the list the element whose number is n" and
"delete from the list the object that is the nth element".
The above accomplishes the latter.   A very perverse fix would be
 (delete (nth 3 the-list) the-list :start 3 :count 1)
I suppose.

This is ultra-low error checking, though, and I think it's disastrous
for anyone to do this kind of thing in production work.  Really one
should have code that determines the list to be of appropriate length,
and that doesn't waste time re-traversing the list every time it has
a question. At element 3, it hardly matters, but at element 100 it does.
From: Marco Antoniotti
Subject: Re: Lisp novice's question
Date: 
Message-ID: <y6c3dgefu1d.fsf@octagon.mrl.nyu.edu>
Kent M Pitman <······@world.std.com> writes:

> Rainer Joswig <······@corporate-world.lisp.de> writes:
> 
> > > The answer is RTFM!
> > > 
> > > 	(defvar the-list '(1 2 2 3 4))
> > > 	(delete (nth 3 the-list) the-list)  ; Count from 0.
> > 
> > How about deleting just the nth element. ;-)
> 
> Well, there ARE two meanings of "delete the nth element".  That is,
> "delete from the list the element whose number is n" and
> "delete from the list the object that is the nth element".
> The above accomplishes the latter.   A very perverse fix would be
>  (delete (nth 3 the-list) the-list :start 3 :count 1)

Well this is obviously better.

> I suppose.
> 
> This is ultra-low error checking, though, and I think it's disastrous
> for anyone to do this kind of thing in production work.  Really one
> should have code that determines the list to be of appropriate length,
> and that doesn't waste time re-traversing the list every time it has
> a question. At element 3, it hardly matters, but at element 100 it
> does.

Of course.  The point of my post was to show how CL does that and more
withouth necessarily resort to pointer hacking.  The right solutions
(using SETF, NTH and CAR/CDR) are left as an exercise :)

Cheers



-- 
Marco Antoniotti =============================================================
NYU Bioinformatics Group			 tel. +1 - 212 - 998 3488
719 Broadway 12th Floor                          fax  +1 - 212 - 995 4122
New York, NY 10003, USA				 http://galt.mrl.nyu.edu/valis
             Like DNA, such a language [Lisp] does not go out of style.
			      Paul Graham, ANSI Common Lisp