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

From: Nevo (sakur.deagod at gmail.com)
Date: Fri May 20 05:51:27 EDT 2011

A quick look.
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".

Nevo

On 20 May 2011 16:48, 김태윤 <kty1104 at gmail.com> wrote:

> hello
> 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
> explorer
> and when I am trying to find
> exp
> then error arise
> when the text.txt contains
> explorers
> and the time I trying to search
> expl
> 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
> 2explorers
> and when I am trying to find
> 2explorer
> no error arise
> but
> 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))
>          #t
>          #f))
>    ;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)
>                 (newline)
>                 (display len)
>                 (newline)
>                 (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)))))
>    (loop))
>  (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)))
>    (loop)))
> (loop)
> ;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)))
> ;show
> (send f show #t)
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
>
> _________________________________________________
>  For list-related administrative tasks:
>  http://lists.racket-lang.org/listinfo/users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20110520/72398788/attachment.html>

Posted on the users mailing list.