[plt-scheme] iterate over characters in string

From: MJ Ray (markj+0111 at cloaked.freeserve.co.uk)
Date: Tue Aug 13 14:01:58 EDT 2002

Chris Uzdavinis <chris at atdesk.com> wrote:
> That would work as well as for-each on a normal list, but I'm looking
> for a function that operates *directly* on a string without having to
> convert it into a list first.

Unless you have a way into the underlying data type, you're not likely to do
better than the primitive.  I would hope that even if you do, you can't beat
the primitive.

>> > It seems a bit verbose (for my taste) and possibly inefficient due to
>> > calling string->list if it really is O(n).
>> Can any iteration over a list be anything other than O(n)?
> Of course not.  But I'm really wanting to avoid O(2n) when O(1n) [...]

Erm, O(2n) = O(n) doesn't it?  Or am I finally starting to forget my theory?

> [...] What you provided is correct but it's still doing the same amount of
> work as my original code does.

What I provided was a literal implementation of your "something like".  As
others have since posted, getting rid of the accumulator is the biggest
improvement that can be made.

Posted on the users mailing list.