[plt-scheme] critique of some code
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