fork(1) download
  1. ; rotated palindrome
  2.  
  3. (define (split n xs)
  4. (let loop ((n n) (xs xs) (zs '()))
  5. (if (or (zero? n) (null? xs))
  6. (values (reverse zs) xs)
  7. (loop (- n 1) (cdr xs) (cons (car xs) zs)))))
  8.  
  9. (define (string-reverse str)
  10. (list->string (reverse (string->list str))))
  11.  
  12. (define (string-rotate n str)
  13. (call-with-values
  14. (lambda () (split n (string->list str)))
  15. (lambda (front back) (list->string (append back front)))))
  16.  
  17. (define (rot-pal? str)
  18. (let loop ((n (string-length str)) (str str))
  19. (if (zero? n) #f
  20. (if (string=? str (string-reverse str)) #t
  21. (loop (- n 1) (string-rotate 1 str))))))
  22.  
  23. (display (rot-pal? "abcdedcba")) (newline)
  24. (display (rot-pal? "dedcbaabc")) (newline)
  25. (display (rot-pal? "abcdefghi")) (newline)
Success #stdin #stdout 0.04s 8280KB
stdin
Standard input is empty
stdout
#t
#t
#f