Os Grupos Google já não suportam novas publicações ou subscrições da Usenet. O conteúdo anterior permanece visível.
Dismiss

why (reverse "aba") not compliing?

10 visualizações
Ir para a primeira mensagem não lida

Hyunchul Kim

não lida,
01/03/1999, 03:00:0001/03/99
para
From a book I saw

reverse is a function for sequences, so I believe it is for strings as
well buy when I tried (reverse "aba") in CL, it did not compile and says
"wrong type argument: consp "aba" "

Any explanation?

Thanks.
- Jay


David B. Lamkins

não lida,
01/03/1999, 03:00:0001/03/99
para
In article <Pine.SOL.3.96L.99030...@caesar.phil.cmu.edu> ,
Hyunchul Kim <hyun...@andrew.cmu.edu> wrote:

You're right. Perhaps your CL implementation is broken. What CL are you
running?

--
David B. Lamkins <http://www.teleport.com/~dlamkins/>

Marco Antoniotti

não lida,
02/03/1999, 03:00:0002/03/99
para

"David B. Lamkins" <dlam...@teleport.com> writes:

> In article <Pine.SOL.3.96L.99030...@caesar.phil.cmu.edu> ,
> Hyunchul Kim <hyun...@andrew.cmu.edu> wrote:
>
> > From a book I saw
> >
> > reverse is a function for sequences, so I believe it is for strings as
> > well buy when I tried (reverse "aba") in CL, it did not compile and says
> > "wrong type argument: consp "aba" "
> >
> > Any explanation?
> >
> > Thanks.
> > - Jay
> >
>
> You're right. Perhaps your CL implementation is broken. What CL are you
> running?

His CL implementation is broken. In CMUCL

* (reverse "abc")
"cba"
*

--
Marco Antoniotti ===========================================
PARADES, Via San Pantaleo 66, I-00186 Rome, ITALY
tel. +39 - (0)6 - 68 10 03 17, fax. +39 - (0)6 - 68 80 79 26
http://www.parades.rm.cnr.it

Erik Naggum

não lida,
02/03/1999, 03:00:0002/03/99
para
* Hyunchul Kim <hyun...@andrew.cmu.edu>

| From a book I saw reverse is a function for sequences, so I believe it
| is for strings as well buy when I tried (reverse "aba") in CL, it did not
| compile and says "wrong type argument: consp "aba" "

it seems that you are using a Lisp that is not Common Lisp at all. your
previous problems with MAPCAR look like Emacs Lisp problems, and the
above error message is also provided by Emacs Lisp. Emacs Lisp is very
far from a Common Lisp implementation. even with the CL package, the
core functions are still broken, but (require 'cl) does give you MAPCAR*,
but no REVERSE*.

#:Erik

Juanma Barranquero

não lida,
02/03/1999, 03:00:0002/03/99
para
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 02 Mar 1999 10:19:28 +0000, Erik Naggum <er...@naggum.no> wrote:

> but (require 'cl) does give you MAPCAR*,
> but no REVERSE*.

Not a full implementation, perhaps, but that should be a start for
Hyumchul Kim:

(defun reverse-array (array)
(let* ((len-array (length array))
(new-array (typecase array
(string (make-string len-array 0))
(vector (make-vector len-array 0))
(bool-vector (make-bool-vector len-array 0))
(t (make-char-table
(char-table-subtipe array))))))
(do ((i 0 (1+ i)))
((>= i len-array) new-array)
(aset new-array i (aref array (- len-array i 1))))))

(defun reverse* (sequence)
(if (arrayp sequence)
(reverse-array sequence)
(reverse sequence)))

(defun nreverse-array (array)
(let* ((len-array (length array))
(pivot (/ len-array 2)))
(do ((i 0 (1+ i)))
((>= i pivot) array)
(rotatef (aref array i) (aref array (- len-array i 1))))))

(defun nreverse* (sequence)
(if (arrayp sequence)
(nreverse-array sequence)
(nreverse sequence)))


/L/e/k/t/u

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 6.0.2i

iQA/AwUBNtvINf4C0a0jUw5YEQILsgCgnzF+u1S/tri9ItFvydVPfvdtZoIAoIAw
ZP7E6uymuRwF9Se/uB/MRcWd
=HNOD
-----END PGP SIGNATURE-----


Pierpaolo Bernardi

não lida,
02/03/1999, 03:00:0002/03/99
para
Hyunchul Kim (hyun...@andrew.cmu.edu) wrote:
: From a book I saw

: reverse is a function for sequences, so I believe it is for strings as
: well buy when I tried (reverse "aba") in CL, it did not compile and says
: "wrong type argument: consp "aba" "

: Any explanation?

The error message is bogus.

But note that (reverse "aba") is trying to modify a constant string.
This is not valid common lisp (but probably this is not the problem here).

P.

David B. Lamkins

não lida,
02/03/1999, 03:00:0002/03/99
para
In article <920388085.138047@fire-int> , bern...@cli.di.unipi.it (Pierpaolo
Bernardi) wrote:

No. Reverse is not destructive. It always creates and returns a new
sequence.

There are many ways to abbreviate something, but only one way not to.

Kent M Pitman

não lida,
02/03/1999, 03:00:0002/03/99
para
bern...@cli.di.unipi.it (Pierpaolo Bernardi) writes:

> But note that (reverse "aba") is trying to modify a constant string.
> This is not valid common lisp (but probably this is not the problem here).

Btw, I can't help noting that "aba" doesn't need reversing.

Erik Naggum

não lida,
02/03/1999, 03:00:0002/03/99
para
* bern...@cli.di.unipi.it (Pierpaolo Bernardi)

| But note that (reverse "aba") is trying to modify a constant string.

you may be thinking about NREVERSE, not REVERSE.

#:Erik

Pierpaolo Bernardi

não lida,
02/03/1999, 03:00:0002/03/99
para Paul Dietz

On Tue, 2 Mar 1999, Paul Dietz wrote:

> REVERSE doesn't modify anything -- NREVERSE does.

OOPS.

Thank you.

P.


Thomas A. Russ

não lida,
02/03/1999, 03:00:0002/03/99
para
bern...@cli.di.unipi.it (Pierpaolo Bernardi) writes:

>
> Hyunchul Kim (hyun...@andrew.cmu.edu) wrote:
> : From a book I saw
>
> : reverse is a function for sequences, so I believe it is for strings as
> : well buy when I tried (reverse "aba") in CL, it did not compile and says
> : "wrong type argument: consp "aba" "
>
> : Any explanation?
>
> The error message is bogus.

Yes. It may indicate the user is not using a full Common Lisp, but
rather a simpler lisp where REVERSE applies only to lists and not to
sequences in general.

> But note that (reverse "aba") is trying to modify a constant string.

> This is not valid common lisp (but probably this is not the problem here).

Actually, this is valid common lisp, since the constant string is not
modified. A new sequence is produced.

If it were the destructive version NREVERSE, it would not be valid.

[Actually, a question for Kent Pitman: The hyperspec states that
"reverse always creates and returns a new sequence". That would
imply that an unbelievably clever implementation could not return
the same sequence given a palindrome: (reverse "aba")]


>
> P.

--
Thomas A. Russ, USC/Information Sciences Institute t...@isi.edu

Barry Margolin

não lida,
02/03/1999, 03:00:0002/03/99
para
In article <ymi4so3...@sevak.isi.edu>,

Thomas A. Russ <t...@sevak.isi.edu> wrote:
>[Actually, a question for Kent Pitman: The hyperspec states that
> "reverse always creates and returns a new sequence". That would
> imply that an unbelievably clever implementation could not return
> the same sequence given a palindrome: (reverse "aba")]

That's correct. If you do:

(setq list1 (list a b a))
(setq list2 (reverse list1))
(setf (car list2) 'c)
(eq (car list1) (car list2)) => NIL

If a "clever" implementation returned the original sequence, this would
return T, which would be incorrect.

Some non-destructive functions are give explicit leeway (or even
requirements) to share structure with the original sequence. For instance,
APPEND is required to copy all but the last argument, which must be used as
is (the CDR of the copy of the 2nd to last argument should point to it --
this allows the last argument to be an improper list or even a non-list),
and the set functions are allowed to share structure. However, REVERSE is
not one of these -- it must produce a fresh list, even if it's EQUAL to the
original.

--
Barry Margolin, bar...@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

Tim Bradshaw

não lida,
03/03/1999, 03:00:0003/03/99
para
t...@sevak.isi.edu (Thomas A. Russ) writes:

> [Actually, a question for Kent Pitman: The hyperspec states that
> "reverse always creates and returns a new sequence". That would
> imply that an unbelievably clever implementation could not return
> the same sequence given a palindrome: (reverse "aba")]

That's right, and that's always what you want. Otherwise this might not work (or even be legal since it's modifying a constant):

(setf (aref (reverse "aba") 0) #\d)

--tim


0 mensagens novas