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

From: Nevo (sakur.deagod at gmail.com)
Date: Fri May 20 06:28:33 EDT 2011

It seems your input data is not right here. I use your code (see below), it
works well (besides the overflow problem).

#lang racket
;it is list operation though parameter is str
;as little like human, check first and last character first
(define count 0)
(define data (string->list "explorers"))
(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)
     (newline)
     (display str)
     (newline)
     (display (drop (take data (+ index len)) index))
     (newline)
     (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))
  (format "Found ~a" count))

(search (string->list "explorer"))

On 20 May 2011 17:51, Nevo <sakur.deagod at gmail.com> wrote:

> 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".
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20110520/5c3f1c51/attachment.html>

Posted on the users mailing list.