revcomp-2.scm 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. ;; The Computer Language Benchmarks Game
  2. ;; http://shootout.alioth.debian.org/
  3. ;; Contributed by Per Bothner
  4. ;; Based on Java version #1 contributed by Anthony Donnefort
  5. ;; slightly modified to read 82 bytes at a time by Razii
  6. (define-constant NL :: int 10) ; #\newline as byte
  7. (define-constant GT ::int 62) ; #\> as byte
  8. (define cmp :: byte[]
  9. (let* ((c (byte[] length: 128))
  10. (from "TAGCVHRMYKBDU")
  11. (to "ATCGBDYKRMVHA")
  12. (i :: int (string-length from)))
  13. (do ((i :: int 0 (+ i 1))) ((>= i 128))
  14. (set! (c i) i))
  15. (let loop ()
  16. (set! i (- i 1))
  17. (let ((f (char->integer (string-ref from i)))
  18. (t (char->integer (string-ref to i))))
  19. (set! (c f) t)
  20. (set! (c (+ f 32)) t))
  21. (if (> i 0)
  22. (loop)))
  23. c))
  24. (define-simple-class ReversibleByteArray (java.io.ByteArrayOutputStream)
  25. class-name: ".RevByteArray"
  26. ((reverse-and-print) ::void ;; throws Exception FIXME
  27. (if (> count 0)
  28. (let ((i ::int 0)
  29. (j ::int (- count 1))
  30. (b ::byte[] buf))
  31. (let loop ()
  32. (let ((old (b i)))
  33. (set! i (+ i 1))
  34. (if (not (= old NL))
  35. (loop))))
  36. (let loop ()
  37. (cond ((<= i j)
  38. (if (= (b i) NL)
  39. (set! i (+ i 1)))
  40. (if (= (b j) NL)
  41. (set! j (- j 1)))
  42. (if (<= i j)
  43. (let ((tmp (b i)))
  44. (set! (b i) (cmp (b j)))
  45. (set! i (+ i 1))
  46. (set! (b j) (cmp tmp))
  47. (set! j (- j 1))))
  48. (loop))))
  49. (java.lang.System:out:write b 0 count)))))
  50. (let ((line (byte[] length: 82))
  51. (buf (ReversibleByteArray)))
  52. (let loop ()
  53. (let ((read (java.lang.System:in:read line)))
  54. (if (>= read 0)
  55. (let ((last ::int 0))
  56. (do ((i ::int 0 (+ i 1))) ((>= i read) #!void)
  57. (cond ((= (line i) GT)
  58. (buf:write line last (- i last))
  59. (buf:reverse-and-print)
  60. (buf:reset)
  61. (set! last i))))
  62. (buf:write line last (- read last))
  63. (loop)))))
  64. (buf:reverse-and-print))