It seems your input data is not right here. I use your code (see below), it works well (besides the overflow problem).<div><br></div><div><div>#lang racket</div><div>;it is list operation though parameter is str</div><div>
;as little like human, check first and last character first</div><div>(define count 0)</div><div>(define data (string->list "explorers"))</div><div>(define (search str)</div><div> (set! count 0)</div><div> (define len (length str))</div>
<div> (define data-len (length data))</div><div> (when (and (not (= 0 len)) (>= data-len len))</div><div> (define first-char (first str))</div><div> (define last-char (last str))</div><div> ;is it exactly same?</div>
<div> (define (exact? str len index)</div><div> (newline)</div><div> (display str)</div><div> (newline)</div><div> (display (drop (take data (+ index len)) index))</div><div> (newline)</div><div> (if (equal? str (drop (take data (+ index len)) index))</div>
<div> #t</div><div> #f))</div><div> ;if first and last character is correct, then compare whole searching string, if not, skip</div><div> (define (loop [index 0])</div><div> (when (> data-len index)</div>
<div> (if (and (equal? first-char (list-ref data index))</div><div> (display index)</div><div> (newline)</div><div> (display len)</div><div> (newline)</div>
<div> (display "==================")</div><div> (equal? last-char (list-ref data (+ index len -1))))</div><div> (when (exact? str len index)</div><div> (set! count (+ count 1))</div>
<div> (loop (+ index len)))</div><div> (loop (+ index 1)))))</div><div> (loop))</div><div> (format "Found ~a" count))</div><div><br></div><div>(search (string->list "explorer"))</div>
<br><div class="gmail_quote">On 20 May 2011 17:51, Nevo <span dir="ltr"><<a href="mailto:sakur.deagod@gmail.com">sakur.deagod@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
A quick look.<div>You're not guarding the list end ( (+ index len - 1) should be also less than data-len), that's why you have index overflow error. Then when you tried to search "2explorer" from "2explorers", the first turn of loop failed, then the second turn of loop failed on "(equal? first-char (list-ref data index))", that's why you cannot find "2explorer".</div>
<div><br></div></blockquote></div><br></div>