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

iterating over string

351 views
Skip to first unread message

Alexander Schofield

unread,
Feb 10, 2002, 7:08:29 PM2/10/02
to
What is a better way of iterating over strings a char at a time? Say a
better way to produce the same result below:

(setq str "dummy string")

(coerce (let ((chlist (coerce str 'list)))
(mapcar (lambda (ch) (code-char (1+ (char-code ch))))
chlist)) 'string)

#=> "evnnz!tusjoh"

TIA
--
Alexander Schofield

Erik Naggum

unread,
Feb 10, 2002, 7:06:22 PM2/10/02
to
* Alexander Schofield <pc...@mailhost.njit.edu>

| What is a better way of iterating over strings a char at a time?

See the function map in the hyperspec.

///
--
In a fight against something, the fight has value, victory has none.
In a fight for something, the fight is a loss, victory merely relief.

Howard Ding

unread,
Feb 10, 2002, 7:06:39 PM2/10/02
to
>
> (setq str "dummy string")
>
> (coerce (let ((chlist (coerce str 'list)))
> (mapcar (lambda (ch) (code-char (1+ (char-code ch))))
> chlist)) 'string)
>
> #=> "evnnz!tusjoh"

You could use the general map function:

(map 'string (lambda (char) (code-char (1+ (char-code char))))
"dummy string")

--
Howard Ding
<had...@att.net>
http://math.sunysb.edu/~hading http://thunder.prohosting.com/~hading

Christopher C. Stacy

unread,
Feb 10, 2002, 11:46:04 PM2/10/02
to
>>>>> On Sun, 10 Feb 2002 19:08:29 -0500, Alexander Schofield ("Alexander") writes:

Alexander> What is a better way of iterating over strings a char at a time? Say a
Alexander> better way to produce the same result below:

Alexander> (setq str "dummy string")

Alexander> (coerce (let ((chlist (coerce str 'list)))
Alexander> (mapcar (lambda (ch) (code-char (1+ (char-code ch))))
Alexander> chlist)) 'string)

Alexander> #=> "evnnz!tusjoh"

A string is one of the "sequence" data types in Lisp, so there's
no need to convert it into a list. The elements in the string
are characters, and there's generally no need to convert them back
and forth into their numeric codes (although that's appropriate
given what your example function accomplishes).

Read the "Sequences" chapter in the Common Lisp HyperSpec,
in particular the function MAP. You might also like to
read about the LOOP macro in the "Iteration" chapter,
especially the "for-as-across" subclause.
MAP includes the functionality of COERCE.

Dr. Edmund Weitz

unread,
Feb 11, 2002, 1:11:28 AM2/11/02
to
Alexander Schofield <pc...@mailhost.njit.edu> writes:

> What is a better way of iterating over strings a char at a time?
> Say a better way to produce the same result below:

<http://agharta.de/cookbook/strings.html#process>

Edi.

--

Dr. Edmund Weitz
Hamburg
Germany

The Common Lisp Cookbook
<http://agharta.de/cookbook/>

Steven M. Haflich

unread,
Feb 11, 2002, 11:57:30 PM2/11/02
to

Alexander Schofield wrote:
>
> What is a better way of iterating over strings a char at a time? Say a
> better way to produce the same result below:

In addition to the worthy advice already posted, see the
ACROSS clause of LOOP.

Jeff Sandys

unread,
Feb 12, 2002, 3:33:37 PM2/12/02
to
I like loop - across,

LISP(46): (setq str "dummy string")
"dummy string"
LISP(47): (loop
:for ch :across str
:collect (code-char (1+ (char-code ch))) :into str-out
:finally (return (coerce str-out 'string)))
"evnnz!tusjoh"

Thanks,
Jeff Sandys

Nils Goesche

unread,
Feb 12, 2002, 4:03:31 PM2/12/02
to
In article <3C697C21...@juno.com>, Jeff Sandys wrote:
> I like loop - across,
>
> LISP(46): (setq str "dummy string")
> "dummy string"
> LISP(47): (loop
> :for ch :across str
> :collect (code-char (1+ (char-code ch))) :into str-out
> :finally (return (coerce str-out 'string)))
> "evnnz!tusjoh"

But at least look again at

(map 'string (lambda (c) ...

Regards,
--
Nils Goesche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x42B32FC9

Steve Long

unread,
Feb 12, 2002, 6:52:10 PM2/12/02
to
Jeff,

Check dovector in UTILS :)

sl

0 new messages