[plt-scheme] critique of some code

From: Sam TH (samth at ccs.neu.edu)
Date: Sun Dec 6 22:47:55 EST 2009

On Sun, Dec 6, 2009 at 10:45 PM, Todd O'Bryan <toddobryan at gmail.com> wrote:
> Thanks for both suggestions. I actually tried (in-range ...), but
> didn't know about for/first.
>
> Here's the regexp-match-positions version. (I knew PLT Scheme did
> regexes, but didn't think about using them.)
>
> (define (string-index-of str sub)
>  (let
>      ([result (regexp-match-positions sub str)])
>    (if result
>        (caar result)
>        -1)))

Note that you need (regexp-match-positions (regexp-quote sub) str), in
case `sub' contains regexp metacharacters.

Also, it would be rather more Schemely in all of these to return #f in
case of failure, instead of -1.  That way your function can be used as
a predicate.
-- 
sam th
samth at ccs.neu.edu


Posted on the users mailing list.