Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Subseq relative to end of sequence

1 view
Skip to first unread message

Jacek Generowicz

unread,
Sep 24, 2002, 4:22:00 AM9/24/02
to
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,

Matthew Danish

unread,
Sep 24, 2002, 5:34:51 AM9/24/02
to
See BUTLAST and NBUTLAST.

--
; Matthew Danish <mda...@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."

Matthew Danish

unread,
Sep 24, 2002, 5:38:10 AM9/24/02
to
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.

Paul F. Dietz

unread,
Sep 24, 2002, 8:02:45 AM9/24/02
to
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

Wolfhard Buß

unread,
Sep 24, 2002, 11:34:45 AM9/24/02
to
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)

Erik Naggum

unread,
Sep 24, 2002, 5:23:54 PM9/24/02
to
* 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.

ozan s yigit

unread,
Sep 24, 2002, 10:55:25 PM9/24/02
to
Erik Naggum <er...@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

Jacek Generowicz

unread,
Sep 25, 2002, 12:34:34 PM9/25/02
to
ozan s yigit <o...@blue.cs.yorku.ca> writes:

> Erik Naggum <er...@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.

Jacek Generowicz

unread,
Sep 25, 2002, 12:40:24 PM9/25/02
to
wb...@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,

Jacek Generowicz

unread,
Sep 25, 2002, 12:42:18 PM9/25/02
to
"Paul F. Dietz" <di...@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.

Vassil Nikolov

unread,
Sep 26, 2002, 12:53:00 AM9/26/02
to
Erik Naggum <er...@naggum.no> wrote in message news:<32418914...@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.

JB

unread,
Sep 27, 2002, 2:41:50 PM9/27/02
to
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 =-----

0 new messages