[ create a new paste ] login | about

Project: programmingpraxis
Link: http://programmingpraxis.codepad.org/e2h1UyvF    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Jan 30:
; string rotation
 
(define (string-find pat str . s)
  (let* ((plen (string-length pat))
         (slen (string-length str))
         (skip (make-vector plen 0)))
    (let loop ((i 1) (j 0))
      (cond ((= i plen))
            ((char=? (string-ref pat i) (string-ref pat j))
              (vector-set! skip i (+ j 1))
              (loop (+ i 1) (+ j 1)))
            ((< 0 j) (loop i (vector-ref skip (- j 1))))
            (else (vector-set! skip i 0)
                  (loop (+ i 1) j))))
    (let loop ((p 0) (s (if (null? s) 0 (car s))))
      (cond ((= s slen) #f)
            ((char=? (string-ref pat p) (string-ref str s))
              (if (= p (- plen 1))
                  (- s plen -1)
                  (loop (+ p 1) (+ s 1))))
            ((< 0 p) (loop (vector-ref skip (- p 1)) s))
            (else (loop p (+ s 1)))))))
 
(define (string-rotated? s1 s2)
  (and (= (string-length s1) (string-length s2))
       (string-find s2 (string-append s1 s1))))
 
(display (string-rotated? "programmingpraxis" "programmingpraxis")) (newline)
(display (string-rotated? "programmingpraxis" "praxisprogramming")) (newline)
(display (string-rotated? "programmingpraxis" "programmingprasix")) (newline)


Output:
1
2
3
0
11
#f


Create a new paste based on this one


Comments: