Programming language shootout: reverse complement

From Gambit wiki

This is a Gambit implementation of the reverse-complement benchmark of the Computer Language Benchmarks Game.

The program


;; The Computer Language Benchmarks Game
;; Derived by Bradley Lucier from the Ikarus variant
;; derived by Michael D. Adams from the MzScheme variant

(declare (standard-bindings)(extended-bindings)(block)(fixnum)(not safe))

(define-macro (unless test . body)
  `(if (not ,test)

(define translation (make-vector 128))

 (lambda (from-to)
   (let* ([char (lambda (sym) (string-ref (symbol->string sym) 0))]
          [from (char (car from-to))]
          [to (char-upcase (char (cadr from-to)))])
     (vector-set! translation (char->integer from) to)
     (vector-set! translation (char->integer (char-upcase from)) to)))
 '([a t]
   [c g]
   [g c]
   [t a]
   [u a]
   [m k]
   [r y]
   [w w]
   [s s]
   [y R]
   [k M]
   [v b]
   [h d]
   [d h]
   [b v]
   [n n]))

(define (put-whole-string s) (write-substring s 0 (string-length s)))

(define (output lines)
  (if (> (length lines) 2)
      (let* ([pos (- (string-length (cadr lines)) (string-length (car lines)))]
              (lambda (s i) (write-substring s 0 i))]
              (lambda (s i) (write-substring s i (string-length s)))])
        (put-whole-string (car lines))
        (put-first-half (cadr lines) pos)
        (let loop ([l (cdr lines)])
           [(null? (cdr l))
            (put-second-half (car l) pos)
            (put-second-half (car l) pos)
            (put-first-half (cadr l) pos)
            (loop (cdr l))])))))

(define (main . args)
  (let loop ([accum '()])
    (let ([l (read-line)])
      (if (eof-object? l)
	  (output accum)
	   [(and (not (zero? (string-length l)))
		 (eqv? #\> (string-ref l 0)))
	    (output accum)
	    (put-whole-string l)
	    (loop '())]
	    (let* ([len (string-length l)]
		   [dest (make-string len)])
	      (let loop ([i 0][j (- len 1)])
		(unless (= i len)
			(string-set! dest j
				     (vector-ref translation
						 (char->integer (string-ref l i))))
			(loop (+ i 1) (- j 1))))
	      (loop (cons dest accum)))]))))


gsc reverse-complement


gsi reverse-complement < > fasta.out is computed by

gsi fasta 2500000 >