[plt-scheme] iterate over characters in string

From: MJ Ray (markj at cloaked.freeserve.co.uk)
Date: Tue Aug 13 10:23:03 EDT 2002

Chris Uzdavinis <chris at atdesk.com> wrote:
>   * is there any kind of for-each function that works on a string at
>     the byte level?

What about map?

> 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)?

> I'm hoping some mechanism does exist that I just overlooked.  Ideally
> I'd do something like this: 

   (define (calc-checksum line)
     (let ((ck 0))
       (map (lambda (c) (set! ck (+ ck (char->integer c))))
            (string->list line))
       (modulo ck 256)))

HTH

MJR




Posted on the users mailing list.