Debian Codegraph @

benchmarks/rnrs-benchmarks/ntakl.ss

raw
1 ;;; NTAKL -- The TAKeuchi function using lists as counters,
2 ;;; with an alternative boolean expression.
3
4 (library (rnrs-benchmarks ntakl)
5 (export main)
6 (import (rnrs) (rnrs-benchmarks))
7
8 (define (listn n)
9 (if (= n 0)
10 '()
11 (cons n (listn (- n 1)))))
12
13 (define l18 (listn 18))
14 (define l12 (listn 12))
15 (define l6 (listn 6))
16
17 (define (mas x y z)
18 (if (not (shorterp y x))
19 z
20 (mas (mas (cdr x) y z)
21 (mas (cdr y) z x)
22 (mas (cdr z) x y))))
23
24 ; Part of the fun of this benchmark is seeing how well the compiler
25 ; can understand this ridiculous code, which dates back to the original
26 ; Common Lisp. So it probably isn't a good idea to improve upon it.
27
28 #;
29 (define (shorterp x y)
30 (and (not (null? y))
31 (or (null? x)
32 (shorterp (cdr x)
33 (cdr y)))))
34
35 ; But SML/NJ runs this benchmark about 15 times as fast when the
36 ; code above is rewritten as follows, so I tried it for Scheme also.
37
38 (define (shorterp x y)
39 (cond ((null? y) #f)
40 ((null? x) #t)
41 (else
42 (shorterp (cdr x) (cdr y)))))
43
44 (define (main . args)
45 (run-benchmark
46 "ntakl"
47 ntakl-iters
48 (lambda (result) (equal? result '(7 6 5 4 3 2 1)))
49 (lambda () (lambda () (mas l18 l12 l6))))))