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
* Alexander Schofield <····@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.
>>>>> 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.
Alexander Schofield <····@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/>
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.
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
"Steven M. Haflich" wrote:
>
> 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,
Check dovector in UTILS :)
sl
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"
>
> Thanks,
> Jeff Sandys
>
> "Steven M. Haflich" wrote:
> >
> > 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.