Debugging scheme中的小型仿人文本搜索程序
我试图在scheme中制作一个类似于人类的文本搜索程序 但是这个程序有时不能正常工作 而且我有好几个小时都抓不到虫子 谁能告诉我我的密码有什么问题吗 搜索文本不是一个好主意吗 当我搜索字符串“exp”时 在只包含字符串“explorer”的文本文件中 出错 它告诉我们找到了0Debugging scheme中的小型仿人文本搜索程序,debugging,search,scheme,Debugging,Search,Scheme,我试图在scheme中制作一个类似于人类的文本搜索程序 但是这个程序有时不能正常工作 而且我有好几个小时都抓不到虫子 谁能告诉我我的密码有什么问题吗 搜索文本不是一个好主意吗 当我搜索字符串“exp”时 在只包含字符串“explorer”的文本文件中 出错 它告诉我们找到了0 (define (search str) (set! count 0) (define len (length str)) ;null character calculating (define data
(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)))
我知道已经四年了,但我很怀念我的计划课,所以我做了一件事。(我会发表评论而不是回答,但我的名声还不够好……而且我可能会少一些。)
编辑:我的意思是这个问题已经四年了,而不是计划课。这可能有点令人毛骨悚然,但话说回来,谁知道三年后我会有什么感觉?在什么意义上它不起作用?它应该做什么。当我试图在只包含“explorer”的文本文件中查找“exp”时,列表引用:索引8对于列表来说太大了:(#\e#\x#\p#\l#\o#\r#\e#\r)是否使用scheme作为项目的重要部分?有这么多的搜索引擎已经建成。另外,您知道unix的reg exp工具吗?Yur“exp”的示例问题可以在unix中通过
grep“exp”文件实现。祝你好运。在Racket:(regexp search#px“exp”str)你最好把你的代码贴上去——里面有很多难看的地方。
(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
'()))