0 Replies - 1146 Views - Last Post: 20 July 2010 - 01:31 AM

#1 Crunch   User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

[Scheme] Naive String Matching

Posted 20 July 2010 - 01:31 AM

Description:
(define text "hell o world he  ll o world wo rld")
(define pattern "wor")

(define noofmatches 0)

(define (innerloop j j-max step g)
  (if (and (<= j j-max)  (eq? (string-ref text g) (string-ref pattern j) ) )
    (begin    
         
      
  (innerloop (+ j step) j-max step (+ g 1 ) )) (if (eq? j (+ 1 j-max) )  (set! noofmatches (+ 1 noofmatches )   ) ) )  )


(define (outerloop i i-max step)
  (if (<= i i-max)
    (begin    
      
     (if (eq? (string-ref text i) (string-ref pattern 0) ) (innerloop 0 (- (string-length pattern ) 1) 1 i) )
         
    (outerloop (+ i step) i-max step))))


(define Naive(lambda (  )
                          
            (outerloop 0  (- (string-length text ) 1) 1 ) 
            (display noofmatches )
               
))



Is This A Good Question/Topic? 0
  • +

Page 1 of 1