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><div>Nevo</div><div><br><div class="gmail_quote">On 20 May 2011 16:48, 김태윤 <span dir="ltr"><<a href="mailto:kty1104@gmail.com">kty1104@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
hello<br>
I am trying to make little human like text searching program<br>
but there's bug that I can't catch for many hours<br>
the bug is<br>
when text.txt file contains<br>
explorer<br>
and when I am trying to find<br>
exp<br>
then error arise<br>
when the text.txt contains<br>
explorers<br>
and the time I trying to search<br>
expl<br>
the error<br>
list-ref: index 9 too large for list: (#\e #\x #\p #\l #\o #\r #\e #\r #\s)<br>
is arise on<br>
(equal? last-char (list-ref data (+ index len -1))))<br>
but the wierd thing is that<br>
when the text.txt file contains<br>
2explorers<br>
and when I am trying to find<br>
2explorer<br>
no error arise<br>
but<br>
finding 2explorers returns 0 Found<br>
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br>
#lang scheme/gui<br>
;it is list operation though parameter is str<br>
;as little like human, check first and last character first<br>
(define (search str)<br>
(set! count 0)<br>
(define len (length str))<br>
(define data-len (length data))<br>
(when (and (not (= 0 len)) (>= data-len len))<br>
(define first-char (first str))<br>
(define last-char (last str))<br>
;is it exactly same?<br>
(define (exact? str len index)<br>
(if (equal? str (drop (take data (+ index len)) index))<br>
#t<br>
#f))<br>
;if first and last character is correct, then compare whole<br>
searching string, if not, skip<br>
(define (loop [index 0])<br>
(when (> data-len index)<br>
(if (and (equal? first-char (list-ref data index))<br>
(display index)<br>
(newline)<br>
(display len)<br>
(newline)<br>
(display "==================")<br>
(equal? last-char (list-ref data (+ index len -1))))<br>
(when (exact? str len index)<br>
(set! count (+ count 1))<br>
(loop (+ index len)))<br>
(loop (+ index 1)))))<br>
(loop))<br>
(send msg set-label (format "~a Found" count)))<br>
<br>
<br>
;counting number<br>
(define count 0)<br>
;define path<br>
(define path (get-file))<br>
;convert path to port<br>
(define port (open-input-file path))<br>
(define data '())<br>
;load the file to list data<br>
(define (loop [line (read-line port)])<br>
(when (not (eof-object? line))<br>
(set! data (append data (string->list line)))<br>
(loop)))<br>
(loop)<br>
;search every time the text changes<br>
(define (cb-txt t e) (search (string->list (send t get-value))))<br>
;define frame<br>
(define f (new frame% (label "text search") (min-width 300)))<br>
;define text field<br>
(define txt (new text-field% (label "type here") (parent f) (callback<br>
(λ (t e) (cb-txt t e)))))<br>
;define message<br>
(define msg (new message% (label "0 Found ") (parent f)))<br>
;show<br>
(send f show #t)<br>
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br>
<br>
_________________________________________________<br>
For list-related administrative tasks:<br>
<a href="http://lists.racket-lang.org/listinfo/users" target="_blank">http://lists.racket-lang.org/listinfo/users</a></blockquote></div><br></div>