shootout benchmarks

svn: r10839
This commit is contained in:
Matthew Flatt 2008-07-19 02:40:34 +00:00
parent bba44e46e8
commit b8672845fc
13 changed files with 347 additions and 350 deletions

View File

@ -0,0 +1,2 @@
#lang scheme/base
(display "hello world\n")

View File

@ -1,61 +1,55 @@
;; The Computer Language Shootout ;; The Computer Language Shootout
;; http://shootout.alioth.debian.org/ ;; http://shootout.alioth.debian.org/
(module k-nucleotide mzscheme #lang scheme/base
(require mzlib/list
mzlib/string
(only srfi/13 string-pad-right))
(define (all-counts len dna) (define (all-counts len dna)
(let ([table (make-hash-table)] (let ([table (make-hasheq)]
[seq (make-string len)]) [seq (make-string len)])
(let loop ([s (- (string-length dna) len)]) (for ([s (in-range (- (string-length dna) len) -1 -1)])
(string-copy! seq 0 dna s (+ s len)) (string-copy! seq 0 dna s (+ s len))
(let ([key (string->symbol seq)]) (let ([key (string->symbol seq)])
(let ([cnt (hash-table-get table key 0)]) (let ([cnt (hash-ref table key 0)])
(hash-table-put! table key (add1 cnt)))) (hash-set! table key (add1 cnt)))))
(unless (zero? s) table))
(loop (sub1 s))))
table))
(define (write-freqs table) (define (write-freqs table)
(let* ([content (hash-table-map table cons)] (let* ([content (hash-map table cons)]
[total (exact->inexact (apply + (map cdr content)))]) [total (exact->inexact (apply + (map cdr content)))])
(for-each (for-each
(lambda (a) (lambda (a)
(printf "~a ~a\n" (printf "~a ~a\n"
(car a) (car a)
(real->decimal-string (* 100 (/ (cdr a) total)) 3))) (real->decimal-string (* 100 (/ (cdr a) total)) 3)))
(sort content (lambda (a b) (> (cdr a) (cdr b))))))) (sort content (lambda (a b) (> (cdr a) (cdr b)))))))
(define (write-one-freq table key) (define (write-one-freq table key)
(let ([cnt (hash-table-get table key 0)]) (let ([cnt (hash-ref table key 0)])
(printf "~a\t~a\n" cnt key))) (printf "~a\t~a\n" cnt key)))
(define dna (define dna
(begin (let ([in (current-input-port)])
;; Skip to ">THREE ..." ;; Skip to ">THREE ..."
(regexp-match #rx#"(?m:^>THREE.*$)" (current-input-port)) (regexp-match #rx#"(?m:^>THREE.*$)" in)
(let ([s (open-output-string)]) (let ([s (open-output-string)])
;; Copy everything but newlines to s: ;; Copy everything but newlines to s:
(let loop () (let loop ()
(when (regexp-match #rx#"\n" (current-input-port) 0 #f s) (when (regexp-match #rx#"\n" in 0 #f s)
(loop))) (loop)))
;; Extract the string from s: ;; Extract the string from s:
(string-upcase (get-output-string s))))) (string-upcase (get-output-string s)))))
;; 1-nucleotide counts: ;; 1-nucleotide counts:
(write-freqs (all-counts 1 dna)) (write-freqs (all-counts 1 dna))
(newline) (newline)
;; 2-nucleotide counts: ;; 2-nucleotide counts:
(write-freqs (all-counts 2 dna)) (write-freqs (all-counts 2 dna))
(newline) (newline)
;; Specific sequences: ;; Specific sequences:
(for-each (lambda (seq) (for-each (lambda (seq)
(write-one-freq (all-counts (string-length seq) dna) (write-one-freq (all-counts (string-length seq) dna)
(string->symbol seq))) (string->symbol seq)))
'("GGT" "GGTA" "GGTATT" "GGTATTTTAATT" "GGTATTTTAATTTATAGT")) '("GGT" "GGTA" "GGTATT" "GGTATTTTAATT" "GGTATTTTAATTTATAGT"))
)

View File

@ -5,11 +5,8 @@
;; Derived from the Chicken variant, which was ;; Derived from the Chicken variant, which was
;; Contributed by Anthony Borla ;; Contributed by Anthony Borla
;; Note: as of version 350, this benchmark spends much of
;; its time GCing; it runs 2 times as fast in mzscheme3m.
;; The version that uses complex number is a little ;; The version that uses complex number is a little
;; more elegant, but slower: ;; more elegant, but much slower:
;; (define (mandelbrot iterations x y n ci) ;; (define (mandelbrot iterations x y n ci)
;; (let ((c (+ (- (/ (* 2.0 x) n) 1.5) ;; (let ((c (+ (- (/ (* 2.0 x) n) 1.5)
;; (* ci 0.0+1.0i)))) ;; (* ci 0.0+1.0i))))
@ -19,62 +16,64 @@
;; [(> (magnitude z) 2.0) 0] ;; [(> (magnitude z) 2.0) 0]
;; [else (loop (add1 i) (+ (* z z) c))])))) ;; [else (loop (add1 i) (+ (* z z) c))]))))
#lang scheme/base
(require scheme/cmdline)
(module mandelbrot mzscheme (define +limit-sqr+ 4.0)
;; ------------------------------- (define +iterations+ 50)
(define +limit-sqr+ 4.0) ;; -------------------------------
(define +iterations+ 50) (define (mandelbrot iterations x y n ci)
(let ((cr (- (/ (* 2.0 x) n) 1.5)))
(let loop ((i 0) (zr 0.0) (zi 0.0))
(if (> i iterations)
1
(let ((zrq (* zr zr))
(ziq (* zi zi)))
(cond
((> (+ zrq ziq) +limit-sqr+) 0)
(else (loop (add1 i)
(+ (- zrq ziq) cr)
(+ (* 2.0 (* zr zi)) ci)))))))))
;; ------------------------------- ;; -------------------------------
(define (mandelbrot iterations x y n ci) (define (main n)
(let ((cr (- (/ (* 2.0 x) n) 1.5))) (let ((out (current-output-port)))
(let loop ((i 0) (zr 0.0) (zi 0.0))
(if (> i iterations)
1
(let ((zrq (* zr zr))
(ziq (* zi zi)))
(cond
((> (+ zrq ziq) +limit-sqr+) 0)
(else (loop (add1 i) (+ (- zrq ziq) cr) (+ (* 2.0 (* zr zi)) ci)))))))))
;; ------------------------------- (fprintf out "P4\n~a ~a\n" n n)
(define (main args) (let loop-y ((y 0))
(let ((n (string->number (vector-ref args 0)))
(out (current-output-port)))
(fprintf out "P4\n~a ~a\n" n n) (when (< y n)
(let loop-y ((y 0)) (let ([ci (- (/ (* 2.0 y) n) 1.0)])
(when (< y n) (let loop-x ((x 0) (bitnum 0) (byteacc 0))
(let ([ci (- (/ (* 2.0 y) n) 1.0)]) (if (< x n)
(let ([bitnum (add1 bitnum)]
[byteacc (+ (arithmetic-shift byteacc 1)
(mandelbrot +iterations+ x y n ci))])
(let loop-x ((x 0) (bitnum 0) (byteacc 0)) (cond
((= bitnum 8)
(write-byte byteacc out)
(loop-x (add1 x) 0 0))
(if (< x n) [else (loop-x (add1 x) bitnum byteacc)]))
(let ([bitnum (add1 bitnum)]
[byteacc (+ (arithmetic-shift byteacc 1)
(mandelbrot +iterations+ x y n ci))])
(cond (begin
((= bitnum 8) (when (positive? bitnum)
(write-byte byteacc out) (write-byte (arithmetic-shift byteacc
(loop-x (add1 x) 0 0)) (- 8 (bitwise-and n #x7)))
out))
[else (loop-x (add1 x) bitnum byteacc)])) (loop-y (add1 y))))))))))
(begin ;; -------------------------------
(when (positive? bitnum)
(write-byte (arithmetic-shift byteacc (- 8 (bitwise-and n #x7))) out))
(loop-y (add1 y)))))))))) (command-line #:args (n)
(main (string->number n)))
;; -------------------------------
(main (current-command-line-arguments)))

View File

@ -146,8 +146,7 @@ Correct output N = 1000 is
(printf "~a~%" (real->decimal-string (energy system) 9)) (printf "~a~%" (real->decimal-string (energy system) 9))
(do ((i 1 (+ i 1))) (for ([i (in-range 0 n)])
((< n i))
(advance system 0.01)) (advance system 0.01))
(printf "~a~%" (real->decimal-string (energy system) 9))) (printf "~a~%" (real->decimal-string (energy system) 9)))

View File

@ -7,44 +7,37 @@
;; Written by Dima Dorfman, 2004 ;; Written by Dima Dorfman, 2004
;; Converted to MzScheme by Brent Fulgham ;; Converted to MzScheme by Brent Fulgham
(module nsieve mzscheme #lang scheme/base
(require (only srfi/13 string-index string-pad)) (require scheme/cmdline)
(define (nsieve m) (define (nsieve m)
(let ((a (make-vector m #t))) (let ((a (make-vector m #t)))
(let loop ((i 2) (n 0)) (let loop ((i 2) (n 0))
(if (< i m) (if (< i m)
(begin (if (vector-ref a i)
(if (vector-ref a i) (begin
(begin (let clear ((j (+ i i)))
(let clear ((j (+ i i))) (when (< j m)
(if (< j m) (vector-set! a j #f)
(begin (clear (+ j i))))
(vector-set! a j #f) (loop (+ 1 i) (+ 1 n)))
(clear (+ j i))))) (loop (+ 1 i) n))
(loop (+ 1 i) (+ 1 n))) n))))
(loop (+ 1 i) n)))
n))))
(define (test n) (define (string-pad s len)
(let* ((m (* (expt 2 n) 10000)) (string-append (make-string (- len (string-length s)) #\space)
(count (nsieve m))) s))
(printf "Primes up to ~a ~a~%"
(string-pad (number->string m) 8)
(string-pad (number->string count) 8))))
(define (main args) (define (test n)
(if (< (vector-length args) 1) (let* ((m (* (expt 2 n) 10000))
(begin (count (nsieve m)))
(display "An argument is required") (newline) 2) (printf "Primes up to ~a ~a\n"
(let ((n (string->number (vector-ref args 0)))) (string-pad (number->string m) 8)
(if (not n) (string-pad (number->string count) 8))))
(begin
(display "An integer is required") (newline) 2)
(begin
(if (>= n 0) (test n))
(if (>= n 1) (test (- n 1)))
(if (>= n 2) (test (- n 2)))
0)))))
(main (current-command-line-arguments))) (define (main n)
(when (>= n 0) (test n))
(when (>= n 1) (test (- n 1)))
(when (>= n 2) (test (- n 2))))
(command-line #:args (n) (main (string->number n)))

View File

@ -4,63 +4,63 @@
;; ;;
;; Adapted from CMUCL code by Dima Dorfman; bit-vector stuff by Alex Shinn; ;; Adapted from CMUCL code by Dima Dorfman; bit-vector stuff by Alex Shinn;
;; cobbled together by felix, converted to MzScheme by Brent Fulgham ;; cobbled together by felix, converted to MzScheme by Brent Fulgham
;; Note: Requires MzScheme 299+
(module nsievebits mzscheme #lang scheme/base
(require scheme/cmdline)
(define (make-bit-vector size) (define (make-bit-vector size)
(let* ((len (quotient (+ size 7) 8)) (let* ((len (quotient (+ size 7) 8))
(res (make-bytes len #b11111111))) (res (make-bytes len #b11111111)))
(let ((off (remainder size 8))) (let ((off (remainder size 8)))
(unless (zero? off) (unless (zero? off)
(bytes-set! res (- len 1) (- (arithmetic-shift 1 off) 1)))) (bytes-set! res (- len 1) (- (arithmetic-shift 1 off) 1))))
res)) res))
(define (bit-vector-ref vec i) (define (bit-vector-ref vec i)
(let ((byte (arithmetic-shift i -3)) (let ((byte (arithmetic-shift i -3))
(off (bitwise-and i #x7))) (off (bitwise-and i #x7)))
(and (< byte (bytes-length vec)) (and (< byte (bytes-length vec))
(not (zero? (bitwise-and (bytes-ref vec byte) (not (zero? (bitwise-and (bytes-ref vec byte)
(arithmetic-shift 1 off))))))) (arithmetic-shift 1 off)))))))
(define (bit-vector-set! vec i x) (define (bit-vector-set! vec i x)
(let ((byte (arithmetic-shift i -3)) (let ((byte (arithmetic-shift i -3))
(off (bitwise-and i #x7))) (off (bitwise-and i #x7)))
(let ((val (bytes-ref vec byte)) (let ((val (bytes-ref vec byte))
(mask (arithmetic-shift 1 off))) (mask (arithmetic-shift 1 off)))
(bytes-set! vec (bytes-set! vec
byte byte
(if x (if x
(bitwise-ior val mask) (bitwise-ior val mask)
(bitwise-and val (bitwise-not mask))))))) (bitwise-and val (bitwise-not mask)))))))
(define (nsievebits m) (define (nsievebits m)
(let ((a (make-bit-vector m))) (let ((a (make-bit-vector m)))
(define (clear i) (define (clear i)
(do ([j (+ i i) (+ j i)]) (do ([j (+ i i) (+ j i)])
((>= j m)) ((>= j m))
(bit-vector-set! a j #f) ) ) (bit-vector-set! a j #f)))
(let ([c 0]) (let ([c 0])
(do ([i 2 (add1 i)]) (do ([i 2 (add1 i)])
((>= i m) c) ((>= i m) c)
(when (bit-vector-ref a i) (when (bit-vector-ref a i)
(clear i) (clear i)
(set! c (add1 c)) ) ) ) ) ) (set! c (add1 c)))))))
(define (string-pad s n) (define (string-pad s len)
(string-append (make-string (- n (string-length s)) #\space) (string-append (make-string (- len (string-length s)) #\space)
s)) s))
(define (test n) (define (test n)
(let ((m (* 10000 (arithmetic-shift 1 n)))) (let* ((m (* (expt 2 n) 10000))
(printf "Primes up to ~a ~a~%" (count (nsievebits m)))
(string-pad (number->string m) 8) (printf "Primes up to ~a ~a\n"
(string-pad (number->string (nsievebits m)) 8)))) (string-pad (number->string m) 8)
(string-pad (number->string count) 8))))
(define (main args) (define (main n)
(let ([n (string->number (vector-ref args 0))]) (when (>= n 0) (test n))
(when (>= n 0) (test n)) (when (>= n 1) (test (- n 1)))
(when (>= n 1) (test (- n 1))) (when (>= n 2) (test (- n 2))))
(when (>= n 2) (test (- n 2)))))
(main (current-command-line-arguments))) (command-line #:args (n) (main (string->number n)))

View File

@ -8,54 +8,48 @@
;; Contributed by Anthony Borla ;; Contributed by Anthony Borla
;; --------------------------------------------------------------------- ;; ---------------------------------------------------------------------
;; Note: as of version 350, this benchmark spends much of #lang scheme/base
;; its time GCing; it runs 1.5 times as fast in mzscheme3m. (require scheme/cmdline)
(module partialsums mzscheme (let ((n (exact->inexact
(require (only mzlib/string real->decimal-string)) (string->number
(command-line #:args (n) n)))))
(let ((n (exact->inexact (let loop ([d 0.0]
(string->number (alt 1) (d2 0) (d3 0) (ds 0) (dc 0)
(vector-ref (current-command-line-arguments) 0)))) (s0 0) (s1 0) (s2 0) (s3 0) (s4 0) (s5 0) (s6 0) (s7 0) (s8 0))
(if (= d n)
(let ([format-result
(lambda (str n)
(printf str (real->decimal-string n 9)))])
(alt 1) (d2 0) (d3 0) (ds 0) (dc 0) (format-result "~a\t(2/3)^k\n" s0)
(s0 0) (s1 0) (s2 0) (s3 0) (s4 0) (s5 0) (s6 0) (s7 0) (s8 0)) (format-result "~a\tk^-0.5\n" s1)
(format-result "~a\t1/k(k+1)\n" s2)
(format-result "~a\tFlint Hills\n" s3)
(format-result "~a\tCookson Hills\n" s4)
(format-result "~a\tHarmonic\n" s5)
(format-result "~a\tRiemann Zeta\n" s6)
(format-result "~a\tAlternating Harmonic\n" s7)
(format-result "~a\tGregory\n" s8))
(let loop ([d 0.0] (let* ((d (+ d 1))
(alt 1) (d2 0) (d3 0) (ds 0) (dc 0) (d2 (* d d))
(s0 0) (s1 0) (s2 0) (s3 0) (s4 0) (s5 0) (s6 0) (s7 0) (s8 0)) (d3 (* d2 d))
(if (= d n #;(+ n 1)) (ds (sin d))
(let ([format-result (dc (cos d))
(lambda (str n)
(printf str (real->decimal-string n 9)))])
(format-result "~a\t(2/3)^k\n" s0) (s0 (+ s0 (expt (/ 2.0 3) (- d 1))))
(format-result "~a\tk^-0.5\n" s1) (s1 (+ s1 (/ 1 (sqrt d))))
(format-result "~a\t1/k(k+1)\n" s2) (s2 (+ s2 (/ 1 (* d (+ d 1)))))
(format-result "~a\tFlint Hills\n" s3) (s3 (+ s3 (/ 1 (* d3 (* ds ds)))))
(format-result "~a\tCookson Hills\n" s4) (s4 (+ s4 (/ 1 (* d3 (* dc dc)))))
(format-result "~a\tHarmonic\n" s5) (s5 (+ s5 (/ 1 d)))
(format-result "~a\tRiemann Zeta\n" s6) (s6 (+ s6 (/ 1 d2)))
(format-result "~a\tAlternating Harmonic\n" s7) (s7 (+ s7 (/ alt d)))
(format-result "~a\tGregory\n" s8)) (s8 (+ s8 (/ alt (- (* 2 d) 1))))
(alt (- alt)))
(let* ((d (+ d 1))
(d2 (* d d))
(d3 (* d2 d))
(ds (sin d))
(dc (cos d))
(s0 (+ s0 (expt (/ 2.0 3) (- d 1))))
(s1 (+ s1 (/ 1 (sqrt d))))
(s2 (+ s2 (/ 1 (* d (+ d 1)))))
(s3 (+ s3 (/ 1 (* d3 (* ds ds)))))
(s4 (+ s4 (/ 1 (* d3 (* dc dc)))))
(s5 (+ s5 (/ 1 d)))
(s6 (+ s6 (/ 1 d2)))
(s7 (+ s7 (/ alt d)))
(s8 (+ s8 (/ alt (- (* 2 d) 1))))
(alt (- alt)))
(loop d (loop d
alt d2 d3 ds dc alt d2 d3 ds dc
s0 s1 s2 s3 s4 s5 s6 s7 s8)))))) s0 s1 s2 s3 s4 s5 s6 s7 s8)))))

View File

@ -31,7 +31,7 @@
(digit k q r s t (sub1 n) row 1)) (digit k q r s t (sub1 n) row 1))
(begin (begin
(printf "~a" y) (printf "~a" y)
(digit k q r s t(sub1 n) row (add1 col))))) (digit k q r s t (sub1 n) row (add1 col)))))
(let-values ([(q r s t) (mk q r s t k)]) (let-values ([(q r s t) (mk q r s t k)])
(digit (add1 k) q r s t n row col)))) (digit (add1 k) q r s t n row col))))
(printf "~a\t:~a\n" (printf "~a\t:~a\n"

View File

@ -8,52 +8,51 @@
;; Contributed by Anthony Borla ;; Contributed by Anthony Borla
;; --------------------------------------------------------------------- ;; ---------------------------------------------------------------------
(module recursive mzscheme #lang scheme/base
(require (only mzlib/string real->decimal-string)) (require scheme/cmdline)
;; ------------------------------- ;; -------------------------------
(define (ack m n) (define (ack m n)
(cond ((zero? m) (+ n 1)) (cond ((zero? m) (+ n 1))
((zero? n) (ack (- m 1) 1)) ((zero? n) (ack (- m 1) 1))
(else (ack (- m 1) (ack m (- n 1)))))) (else (ack (- m 1) (ack m (- n 1))))))
;; -------------- ;; --------------
(define (fib n) (define (fib n)
(cond ((< n 2) 1) (cond ((< n 2) 1)
(else (+ (fib (- n 2)) (fib (- n 1)))))) (else (+ (fib (- n 2)) (fib (- n 1))))))
(define (fibflt n) (define (fibflt n)
(cond ((< n 2.0) 1.0) (cond ((< n 2.0) 1.0)
(else (+ (fibflt (- n 2.0)) (fibflt (- n 1.0)))))) (else (+ (fibflt (- n 2.0)) (fibflt (- n 1.0))))))
;; -------------- ;; --------------
(define (tak x y z) (define (tak x y z)
(cond ((not (< y x)) z) (cond ((not (< y x)) z)
(else (tak (tak (- x 1) y z) (tak (- y 1) z x) (tak (- z 1) x y))))) (else (tak (tak (- x 1) y z) (tak (- y 1) z x) (tak (- z 1) x y)))))
(define (takflt x y z) (define (takflt x y z)
(cond ((not (< y x)) z) (cond ((not (< y x)) z)
(else (takflt (takflt (- x 1.0) y z) (takflt (- y 1.0) z x) (takflt (- z 1.0) x y))))) (else (takflt (takflt (- x 1.0) y z) (takflt (- y 1.0) z x) (takflt (- z 1.0) x y)))))
;; ------------------------------- ;; -------------------------------
(define (main args) (define (main n)
(let ((n (string->number (vector-ref args 0))))
(printf "Ack(3,~A): ~A~%" n (ack 3 n)) (printf "Ack(3,~A): ~A~%" n (ack 3 n))
(printf "Fib(~a): ~a~%" (printf "Fib(~a): ~a~%"
(real->decimal-string (+ 27.0 n) 1) (real->decimal-string (+ 27.0 n) 1)
(real->decimal-string (fibflt (+ 27.0 n)) 1)) (real->decimal-string (fibflt (+ 27.0 n)) 1))
(set! n (- n 1)) (set! n (- n 1))
(printf "Tak(~A,~A,~A): ~A~%" (* n 3) (* n 2) n (tak (* n 3) (* n 2) n)) (printf "Tak(~A,~A,~A): ~A~%" (* n 3) (* n 2) n (tak (* n 3) (* n 2) n))
(printf "Fib(3): ~A~%" (fib 3)) (printf "Fib(3): ~A~%" (fib 3))
(printf "Tak(3.0,2.0,1.0): ~a~%" (real->decimal-string (takflt 3.0 2.0 1.0) 1)))) (printf "Tak(3.0,2.0,1.0): ~a~%" (real->decimal-string (takflt 3.0 2.0 1.0) 1)))
;; ------------------------------- ;; -------------------------------
(main (current-command-line-arguments))) (main (command-line #:args (n) (string->number n)))

View File

@ -10,80 +10,79 @@
;; Contributed by Anthony Borla ;; Contributed by Anthony Borla
;; --------------------------------------------------------------------- ;; ---------------------------------------------------------------------
(module regexpdna mzscheme #lang scheme/base
(require scheme/port)
(require mzlib/port) ;; -------------------------------
;; ------------------------------- (define VARIANTS
'(#"agggtaaa|tttaccct" #"[cgt]gggtaaa|tttaccc[acg]" #"a[act]ggtaaa|tttacc[agt]t"
(define VARIANTS #"ag[act]gtaaa|tttac[agt]ct" #"agg[act]taaa|ttta[agt]cct" #"aggg[acg]aaa|ttt[cgt]ccct"
'(#"agggtaaa|tttaccct" #"[cgt]gggtaaa|tttaccc[acg]" #"a[act]ggtaaa|tttacc[agt]t" #"agggt[cgt]aa|tt[acg]accct" #"agggta[cgt]a|t[acg]taccct" #"agggtaa[cgt]|[acg]ttaccct"))
#"ag[act]gtaaa|tttac[agt]ct" #"agg[act]taaa|ttta[agt]cct" #"aggg[acg]aaa|ttt[cgt]ccct"
#"agggt[cgt]aa|tt[acg]accct" #"agggta[cgt]a|t[acg]taccct" #"agggtaa[cgt]|[acg]ttaccct"))
(define IUBS (define IUBS
'((#"B" #"(c|g|t)") (#"D" #"(a|g|t)") (#"H" #"(a|c|t)") '((#"B" #"(c|g|t)") (#"D" #"(a|g|t)") (#"H" #"(a|c|t)")
(#"K" #"(g|t)") (#"M" #"(a|c)") (#"N" #"(a|c|g|t)") (#"K" #"(g|t)") (#"M" #"(a|c)") (#"N" #"(a|c|g|t)")
(#"R" #"(a|g)") (#"S" #"(c|g)") (#"V" #"(a|c|g)") (#"R" #"(a|g)") (#"S" #"(c|g)") (#"V" #"(a|c|g)")
(#"W" #"(a|t)") (#"Y" #"(c|t)"))) (#"W" #"(a|t)") (#"Y" #"(c|t)")))
;; ------------------------------- ;; -------------------------------
(define (ci-byte-regexp s) (define (ci-byte-regexp s)
(byte-regexp (bytes-append #"(?i:" s #")"))) (byte-regexp (bytes-append #"(?i:" s #")")))
;; ------------------------------- ;; -------------------------------
(define (match-count str rx offset cnt) (define (match-count str rx offset cnt)
(let ([m (regexp-match-positions rx str offset)]) (let ([m (regexp-match-positions rx str offset)])
(if m (if m
(match-count str rx (cdar m) (add1 cnt)) (match-count str rx (cdar m) (add1 cnt))
cnt))) cnt)))
;; -------------- ;; --------------
(define (replace-all rx str new) (define (replace-all rx str new)
(let ([out (open-output-bytes)]) (let ([out (open-output-bytes)])
(let loop ([pos 0]) (let loop ([pos 0])
(let ([m (regexp-match-positions rx str pos)]) (let ([m (regexp-match-positions rx str pos)])
(if m (if m
(begin (begin
(write-bytes str out pos (caar m)) (write-bytes str out pos (caar m))
(write-bytes new out) (write-bytes new out)
(loop (cdar m))) (loop (cdar m)))
(write-bytes str out pos)))) (write-bytes str out pos))))
(get-output-bytes out))) (get-output-bytes out)))
;; ------------------------------- ;; -------------------------------
(define (input->bytes) (define (input->bytes)
(let ([b (open-output-bytes)]) (let ([b (open-output-bytes)])
(copy-port (current-input-port) b) (copy-port (current-input-port) b)
(get-output-bytes b))) (get-output-bytes b)))
;; ------------------------------- ;; -------------------------------
;; Load sequence and record its length ;; Load sequence and record its length
(let* ([orig (input->bytes)] (let* ([orig (input->bytes)]
[filtered (replace-all #rx#"(>.*?\n)|\n" orig #"")]) [filtered (replace-all #rx#"(>.*?\n)|\n" orig #"")])
;; Perform regexp counts ;; Perform regexp counts
(for-each (for-each
(lambda (i) (lambda (i)
(printf "~a ~a\n" i (match-count filtered (ci-byte-regexp i) 0 0))) (printf "~a ~a\n" i (match-count filtered (ci-byte-regexp i) 0 0)))
VARIANTS) VARIANTS)
;; Perform regexp replacements, and record sequence length ;; Perform regexp replacements, and record sequence length
(let ([replaced (let ([replaced
(let loop ([sequence filtered] (let loop ([sequence filtered]
[IUBS IUBS]) [IUBS IUBS])
(if (null? IUBS) (if (null? IUBS)
sequence sequence
(loop (replace-all (byte-regexp (caar IUBS)) sequence (cadar IUBS)) (loop (replace-all (byte-regexp (caar IUBS)) sequence (cadar IUBS))
(cdr IUBS))))]) (cdr IUBS))))])
;; Print statistics ;; Print statistics
(printf "~%~A~%~A~%~A~%" (printf "~%~A~%~A~%~A~%"
(bytes-length orig) (bytes-length orig)
(bytes-length filtered) (bytes-length filtered)
(bytes-length replaced))))) (bytes-length replaced))))

View File

@ -27,7 +27,7 @@
;; return element i,j of infinite matrix A ;; return element i,j of infinite matrix A
(define (A i j) (define (A i j)
(/ 1.0 (+ (* (+ i j) (/ (+ i j 1) 2)) i 1))) (/ 1.0 (+ (* (+ i j) (/ (+ i (+ j 1)) 2)) (+ i 1))))
;; multiply vector v by matrix A ;; multiply vector v by matrix A
(define (MultiplyAv n v Av) (define (MultiplyAv n v Av)

View File

@ -1,10 +1,5 @@
;;; http://shootout.alioth.debian.org/ #lang scheme/base
;;;
;;; Contributed by Eli Barzilay
(module sumcol mzscheme (for/fold ([acc 0])
(let loop ([acc 0]) ([n (in-lines)])
(let ([n (read)]) (+ acc (string->number n)))
(if (eof-object? n)
(printf "~a\n" acc)
(loop (+ acc n))))))

View File

@ -0,0 +1,23 @@
#lang scheme/base
(require scheme/cmdline)
;; Each thread runs this loop:
(define (run id next)
(let ([v (thread-receive)])
(cond
[(zero? v) ;; Done
(printf "~a\n" id)
(exit)]
[else ;; Keep going
(thread-send next (sub1 v))
(run id next)])))
(let ([n (command-line #:args (n) (string->number n))])
;; The original thread is #503. Create the rest:
(let ([t1 (for/fold ([next (current-thread)])
([id (in-range 502 0 -1)])
(thread (lambda () (run id next))))])
;; Start:
(thread-send t1 n)
(run 503 t1)))