From: Jacek Generowicz
Subject: Subseq relative to end of sequence
Date: 
Message-ID: <tyfvg4vx0ev.fsf@pcitapi22.cern.ch>
Is there a built-in way, or an idiom for removing the last N elements
form a sequence (in my partictular case, a string) ?

In other words, would you recommend using something other than the
following function (or its body) ?

(defun remove-last-n (sequence n)
  (subseq sequence 0 (- (length sequence) n)))


Thanks,

From: Matthew Danish
Subject: Re: Subseq relative to end of sequence
Date: 
Message-ID: <20020924053451.F10389@lain.res.cmu.edu>
See BUTLAST and NBUTLAST.

-- 
; Matthew Danish <·······@andrew.cmu.edu>
; OpenPGP public key: C24B6010 on keyring.debian.org
; Signed or encrypted mail welcome.
; "There is no dark side of the moon really; matter of fact, it's all dark."
From: Matthew Danish
Subject: Re: Subseq relative to end of sequence
Date: 
Message-ID: <20020924053810.G10389@lain.res.cmu.edu>
Sorry, I guess I'm a bit tired, didn't realize you were talking about
*sequence* functions.  Those two I said only work on lists.

On Tue, Sep 24, 2002 at 05:34:51AM -0400, Matthew Danish wrote:
> See BUTLAST and NBUTLAST.

-- 
; Matthew Danish <·······@andrew.cmu.edu>
; OpenPGP public key: C24B6010 on keyring.debian.org
; Signed or encrypted mail welcome.
; "There is no dark side of the moon really; matter of fact, it's all dark."
From: Paul F. Dietz
Subject: Re: Subseq relative to end of sequence
Date: 
Message-ID: <3D905581.94E8A71F@dls.net>
Jacek Generowicz wrote:
> 
> Is there a built-in way, or an idiom for removing the last N elements
> form a sequence (in my partictular case, a string) ?

(remove-if (constantly t) s :count n :from-end t)
;;; For a string, you can replace (constantly t) with #'identity


(subseq s 0 (max 0 (- (length s) n)))

	Paul
From: Jacek Generowicz
Subject: Re: Subseq relative to end of sequence
Date: 
Message-ID: <tyfznu6t40l.fsf@pcitapi22.cern.ch>
"Paul F. Dietz" <·····@dls.net> writes:

> Jacek Generowicz wrote:
> > 
> > Is there a built-in way, or an idiom for removing the last N elements
> > form a sequence (in my partictular case, a string) ?
> 
> (remove-if (constantly t) s :count n :from-end t)

Ah, indeed, thanks for reminding me about the remove* keyword
parameters, which I have been pretty much neglecting ...

> (subseq s 0 (max 0 (- (length s) n)))

... and for spotting my failure to check the validity of the argument.
From: Wolfhard Buß
Subject: Re: Subseq relative to end of sequence
Date: 
Message-ID: <m3hegf4d0q.fsf@buss-14250.user.cis.dfn.de>
Jacek Generowicz
> Is there a built-in way, or an idiom for removing the last N elements
> from a sequence (in my particular case, a string) ?

If a displaced string is an option, then you might use

 (defun nsub-string (string &key (start 0) (end (length string)))
  (make-array  (- end start)
               :element-type 'character
               :displaced-to string
               :displaced-index-offset start))

or something similar.

How about a string with a fill pointer?

-- 
"I believe in the horse. The automobile is a passing phenomenon."
                              --  Kaiser Wilhelm II. (1859-1941)
From: Jacek Generowicz
Subject: Re: Subseq relative to end of sequence
Date: 
Message-ID: <tyf4rceuio7.fsf@pcitapi22.cern.ch>
·····@gmx.net (Wolfhard Bu�) writes:

> Jacek Generowicz
> > Is there a built-in way, or an idiom for removing the last N elements
> > from a sequence (in my particular case, a string) ?
> 
> If a displaced string is an option, then you might use
> 
>  (defun nsub-string (string &key (start 0) (end (length string)))
>   (make-array  (- end start)
>                :element-type 'character
>                :displaced-to string
>                :displaced-index-offset start))
> 
> or something similar.
> 
> How about a string with a fill pointer?

Could do the job, but it would require altering any code that might
have given me the string in the first place ... so it's not a general
solution.

I was just wondering whether I had overlooked something simple.

Thanks,
From: Erik Naggum
Subject: Re: Subseq relative to end of sequence
Date: 
Message-ID: <3241891434120976@naggum.no>
* Jacek Generowicz
| Is there a built-in way, or an idiom for removing the last N elements
| form a sequence (in my partictular case, a string) ?

  The language could have been defined such that a negative index would be
  interpreted to be from the end rather than from the beginning of a sequence.
  I sometimes think it should, since negative indices have no meaning now.

-- 
Erik Naggum, Oslo, Norway

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.
From: ozan s yigit
Subject: Re: Subseq relative to end of sequence
Date: 
Message-ID: <vi48z1q22xu.fsf@blue.cs.yorku.ca>
Erik Naggum <····@naggum.no> writes:

|* Jacek Generowicz
|| Is there a built-in way, or an idiom for removing the last N elements
|| form a sequence (in my partictular case, a string) ?
|
|  The language could have been defined such that a negative index would be
|  interpreted to be from the end rather than from the beginning of a sequence.
|  I sometimes think it should, since negative indices have no meaning now.

as a bit of trivia, icon is one language that uses non-positive indexes
to address the character positions in a string from right to left. this
would be written (if memory serves) as str[-N:0] in icon.

oz
---
a nought, an ought, a knot, a not easily perceived distinction. -- t. duff
From: Jacek Generowicz
Subject: Re: Subseq relative to end of sequence
Date: 
Message-ID: <tyf8z1quixx.fsf@pcitapi22.cern.ch>
ozan s yigit <··@blue.cs.yorku.ca> writes:

> Erik Naggum <····@naggum.no> writes:
> 
> |* Jacek Generowicz
> || Is there a built-in way, or an idiom for removing the last N elements
> || form a sequence (in my partictular case, a string) ?
> |
> |  The language could have been defined such that a negative index
> |  would be interpreted to be from the end rather than from the
> |  beginning of a sequence.  I sometimes think it should, since
> |  negative indices have no meaning now.

Python uses negative indices to refer to displacement from the end of
sequences, so that seq[-1] refers to the last element of seq, seq[:-3]
gives all but the last 3, seq[-3:] gives the last 3 ...

> as a bit of trivia, icon is one language that uses non-positive indexes
> to address the character positions in a string from right to left. this
> would be written (if memory serves) as str[-N:0] in icon.

... and such a reversal notation is about to appear in the next
version.
From: JB
Subject: Re: Subseq relative to end of sequence
Date: 
Message-ID: <3d94a458_3@news.newsgroups.com>
Jacek Generowicz wrote:
> Python uses negative indices to refer to displacement from
> the end of sequences, so that seq[-1] refers to the last
> element of seq, seq[:-3] gives all but the last 3,
> seq[-3:] gives the last 3 ...
> 

When I started with Lisp, the first thing I did was that I 
wrote two functions "index" and "slice" for python-like 
indexing and slicing. This is extremely simple and you 
could even have Fortran-like indeces very easily.

-- 
Janos Blazi


-----------== Posted via Newsfeed.Com - Uncensored Usenet News ==----------
   http://www.newsfeed.com       The #1 Newsgroup Service in the World!
-----= Over 100,000 Newsgroups - Unlimited Fast Downloads - 19 Servers =-----
From: Vassil Nikolov
Subject: Re: Subseq relative to end of sequence
Date: 
Message-ID: <f34a0f4f.0209252052.58016540@posting.google.com>
Erik Naggum <····@naggum.no> wrote in message news:<················@naggum.no>...
>   The language could have been defined such that a negative index would be
>   interpreted to be from the end rather than from the beginning of a sequence.
>   I sometimes think it should, since negative indices have no meaning now.

I believe they have---as indices of `positions' that are
outside of any possible sequence.  Thus they can be used
as return values to indicate, for example, an absence of
an element satisfying a condition, in a more informative
way than a boolean value.  Of course, it is debatable if
the same should not be achieved with multiple values.

For example:

  (locate-in-sorted-sequence 'c '(a c e g)) => 1
                                            OR t 1

  (locate-in-sorted-sequence 'd '(a c e g)) => -2
                                            OR nil 2

Generally speaking, I believe interpreting negative
indices from the end would be a rather mixed blessing.
It would be much more interesting (though probably not
worth the effort) to make them work in conjunction with
displaced arrays.  (If a1 is displaced to a0, and
(aref a1 0) maps to (aref a0 n), then map (aref a1 -1)
to (aref a0 (- n 1)), etc.)  In C, of course, this comes
`for free,' and arrays in Algol and friends can have an
arbitrary range of integers as indices.

---Vassil.