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 <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."
(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
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)
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.
|* 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
> 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
> > 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 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.
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.
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 =-----