(require srfi/1)
(define (expand-from-center s l r)
(let ([terminals-equal?
(equal? (string-ref s l)
(string-ref s r))])
;;(printf " ~v ~v ~v ~v ~v " l r (+ (- r l) 1) (string-ref s l) (string-ref s r))
(cond [(and terminals-equal? (or (= l 0) (= r (- (string-length s) 1)))) (+ (- r l) 1)]
[terminals-equal? (expand-from-center s (- l 1) (+ 1 r))]
[else (- (- r l) 1)]
)))
(define
(longest-palindrome s)
(if (> (string-length s) 1)
(let* ([sizes (map (lambda (i) (max (expand-from-center s i i)
(expand-from-center s i (+ i 1))))
(range (- (string-length s) 1)))]
[max-size (apply max sizes)]
[max-size-index (list-index (curry equal? max-size) sizes)]
)
(printf " ~v ~v ~v ~v " max-size max-size-index (+ (- 1 (remainder max-size 2))
(- max-size-index (quotient max-size 2)))
(+ max-size-index (quotient max-size 2) 1)
)
(substring s (+ (- 1 (remainder max-size 2)) (- max-size-index (quotient max-size 2)))
(+ max-size-index (quotient max-size 2) 1)))
s))