[racket] I am trying to make little human like text searching program

From: 김태윤 (kty1104 at gmail.com)
Date: Fri May 20 04:48:10 EDT 2011

I am trying to make little human like text searching program
but there's bug that I can't catch for many hours
the bug is
when text.txt file contains
and when I am trying to find
then error arise
when the text.txt contains
and the time I trying to search
the error
list-ref: index 9 too large for list: (#\e #\x #\p #\l #\o #\r #\e #\r #\s)
is arise on
(equal? last-char (list-ref data (+ index len -1))))
but the wierd thing is that
when the text.txt file contains
and when I am trying to find
no error arise
finding 2explorers returns 0 Found
#lang scheme/gui
;it is list operation though parameter is str
;as little like human, check first and last character first
(define (search str)
  (set! count 0)
  (define len (length str))
  (define data-len (length data))
  (when (and (not (= 0 len)) (>= data-len len))
    (define first-char (first str))
    (define last-char (last str))
    ;is it exactly same?
    (define (exact? str len index)
      (if (equal? str (drop (take data (+ index len)) index))
    ;if first and last character is correct, then compare whole
searching string, if not, skip
    (define (loop [index 0])
      (when (> data-len index)
        (if (and (equal? first-char (list-ref data index))
                 (display index)
                 (display len)
                 (display "==================")
                 (equal? last-char (list-ref data (+ index len -1))))
            (when (exact? str len index)
              (set! count (+ count 1))
              (loop (+ index len)))
            (loop (+ index 1)))))
  (send msg set-label (format "~a Found" count)))

;counting number
(define count 0)
;define path
(define path (get-file))
;convert path to port
(define port (open-input-file path))
(define data '())
;load the file to list data
(define (loop [line (read-line port)])
  (when (not (eof-object? line))
    (set! data (append data (string->list line)))
;search every time the text changes
(define (cb-txt t e) (search (string->list (send t get-value))))
;define frame
(define f (new frame% (label "text search") (min-width 300)))
;define text field
(define txt (new text-field% (label "type here") (parent f) (callback
(λ (t e) (cb-txt t e)))))
;define message
(define msg (new message% (label "0 Found         ") (parent f)))
(send f show #t)

Posted on the users mailing list.