fork(1) download
  1. ; identifying palindromes
  2.  
  3. (define (palindrome? xv)
  4. (let ((len (vector-length xv)))
  5. (let loop ((left 0) (right (- len 1)))
  6. (cond ((< right left) #t)
  7. ((not (equal? (vector-ref xv left)
  8. (vector-ref xv right))) #f)
  9. (else (loop (+ left 1) (- right 1)))))))
  10.  
  11. (display (palindrome? (vector 1 2 3 4 5 6 7))) (newline)
  12. (display (palindrome? (vector 1 2 3 4 3 2 1))) (newline)
  13.  
  14. (define (palindrome? xs)
  15. (let loop ((h xs) (t xs) (front (list)))
  16. (cond ((null? t) #t)
  17. ((pair? h)
  18. (if (pair? (cdr h))
  19. (loop (cddr h) (cdr t)
  20. (cons (car t) front))
  21. (loop (cdr h) (cdr t) front)))
  22. ((equal? (car t) (car front))
  23. (loop h (cdr t) (cdr front)))
  24. (else #f))))
  25.  
  26. (display (palindrome? (list 1 2 3 4 5 6 7))) (newline)
  27. (display (palindrome? (list 1 2 3 4 3 2 1))) (newline)
Success #stdin #stdout 0s 7276KB
stdin
Standard input is empty
stdout
#f
#t
#f
#t