方案中的小人喜欢文本搜索程序

发布于 2024-11-08 07:33:31 字数 1219 浏览 0 评论 0原文

我正在尝试在方案中制作像人类一样的文本搜索程序

,但是这个程序有时无法正常工作

,而且我好几个小时都无法捕获错误,

有人可以告诉我我的代码有什么问题吗?

搜索文本不是个好主意吗?

当我搜索字符串“exp”时 在文本文件中,仅包含字符串“explorer” 出现错误 它告诉 Found 0

(define (search str)
  (set! count 0)
  (define len (length str))
  ;null character calculating 
  (define data-len (- (length data) 1))
  ;when string length is less than or equal to data-length
  (when (and (not (= 0 len)) (>= data-len len))
    (define first-char (first str))
    (define last-char (last str))
    ;is it correct?
    (define (exact? str len index)
      (if (equal? str (drop (take data (+ index len)) index))
          #t
          #f))
    ;check first and last character of string if correct, check whether this string is correct completely, if so, skip to next index
    (define (loop [index 0])
      (when (> data-len index)
        (if (and (equal? first-char (list-ref data index)) 
                 (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 "Found : ~a" count)))

I am trying to make little human like text searching program in scheme

but this program doesn't work properly time to time

and I can't catch the bug for many hours

could somebody tell me what's wrong with my code?

and is it not that good idea for searching text?

when I search the string "exp"
in the text file which contain nothing but just string "explorer"
error arise
and it tells Found 0

(define (search str)
  (set! count 0)
  (define len (length str))
  ;null character calculating 
  (define data-len (- (length data) 1))
  ;when string length is less than or equal to data-length
  (when (and (not (= 0 len)) (>= data-len len))
    (define first-char (first str))
    (define last-char (last str))
    ;is it correct?
    (define (exact? str len index)
      (if (equal? str (drop (take data (+ index len)) index))
          #t
          #f))
    ;check first and last character of string if correct, check whether this string is correct completely, if so, skip to next index
    (define (loop [index 0])
      (when (> data-len index)
        (if (and (equal? first-char (list-ref data index)) 
                 (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 "Found : ~a" count)))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人 2024-11-15 07:33:31

我知道已经过去四年了,但我很怀念我的 SCHEME 课程,所以我做了一个东西。 (我会发表评论而不是回答,但我还没有足够的声誉......而且我的声誉可能会更少。)

(define (find-pattern pat str); Returns a list of locations of PATturn in STRing.
  (define (pattern-found? pat-list str-list); Is the pattern (pat-list) at the beginning of this string (str-list)? Also, they're lists, now.
    (cond ((null? pat-list) #t); The base case for the recursion.
          ((null? str-list) #f); Obvious
          ((eq? (car pat-list) (car str-list)); First letter matches
           (pattern-found? (cdr pat-list) (cdr str-list))); Recurse
          (else #f)))
  (define (look-for-pattern pat-list str-list counter results-list)
    (cond ((null? str-list) results-list); Base case
          ((pattern-found? pat-list str-list)
           (look-for-pattern pat-list
                             (cdr str-list)
                             (+ counter 1)
                             (cons counter results-list)))
          (else (look-for-pattern pat-list
                                  (cdr str-list)
                                  (+ counter 1)
                                  results-list))))
  (look-for-pattern (string->list pat)
                    (string->list str)
                    0
                    '()))

编辑:我的意思是自问题提出以来已经四年了,而不是自计划课以来。这有点令人毛骨悚然,但话又说回来,谁知道三年后我会感觉如何?

I know it's been four years, but I'm nostalgic for my SCHEME class, so I made a thing. (I'd comment instead of answering, but I don't have enough reputation yet. ... And I'm probably about to have less.)

(define (find-pattern pat str); Returns a list of locations of PATturn in STRing.
  (define (pattern-found? pat-list str-list); Is the pattern (pat-list) at the beginning of this string (str-list)? Also, they're lists, now.
    (cond ((null? pat-list) #t); The base case for the recursion.
          ((null? str-list) #f); Obvious
          ((eq? (car pat-list) (car str-list)); First letter matches
           (pattern-found? (cdr pat-list) (cdr str-list))); Recurse
          (else #f)))
  (define (look-for-pattern pat-list str-list counter results-list)
    (cond ((null? str-list) results-list); Base case
          ((pattern-found? pat-list str-list)
           (look-for-pattern pat-list
                             (cdr str-list)
                             (+ counter 1)
                             (cons counter results-list)))
          (else (look-for-pattern pat-list
                                  (cdr str-list)
                                  (+ counter 1)
                                  results-list))))
  (look-for-pattern (string->list pat)
                    (string->list str)
                    0
                    '()))

EDIT: I mean it's been four years since the question, not since SCHEME class. That'd be a little creepy, but then again, who knows how I'll feel in three years?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文