[plt-scheme] iterate over characters in string

From: Chris Uzdavinis (chris at atdesk.com)
Date: Tue Aug 13 15:08:45 EDT 2002

MJ Ray <markj+0111 at cloaked.freeserve.co.uk> writes:

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

Yes, O(2n) is still O(n).  I only put it that way to emphasize what I
was trying to avoid... multiple passes.

I mistakenly thought that string-length acted like the C function
strlen(), which was also linear.  As it has been pointed out to me,
the scheme strings keep their lengths in the datatype, so asking the
length is not expensive, and then accessing all the bytes in the range
using string-ref is probably just as good, without any transformation.

> > [...] 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.

I appreciate all the help from everyone.  Since this is just the start
of the project I've decided to tackle in Scheme (making a client
program to our order entry system), I'll probably have more questions
along the way.  :)


Posted on the users mailing list.