#lang scheme
;; Mini-benchmark for range Nat Nat -> [Listof Nat]
;; (range 0 5) => (list 0 1 2 3 4 5)
;; (range 5 0) => (list 5 4 3 2 1 0)
(define (it:build-list i f)
  (let loop ([i (sub1 i)] [a empty])
    (cond [(< i 0) a]
          [else (loop (sub1 i)
                      (cons (f i) a))]))) 
;; using build-list
(define (range-rbl lo hi)
   (if (>= hi lo)
       (build-list (+ (- hi lo) 1) (lambda (i) (+ lo i)))
       (build-list (+ (- lo hi) 1) (lambda (i) (- lo i)))))

;; same as above, but with iterative build-list
(define (range-ibl lo hi)
   (if (>= hi lo)
       (it:build-list (+ (- hi lo) 1) (lambda (i) (+ lo i)))
       (it:build-list (+ (- lo hi) 1) (lambda (i) (- lo i)))))

;; using sequences
(define (range-seq L H)
  (for/list ((i (if (>= H L) 
                    (in-range L (+ H 1)) 
                    (in-range L (- H 1) -1)))) i))

(define ranges (list range-rbl range-ibl range-seq))

(define (run j i)
  (printf "~a~n" `(for ((j (in-range ,j))) (range 0 ,i)))
  (for-each 
   (lambda (r)
     (printf "~a: " (object-name r))
     (collect-garbage)
     (time (void (for ((_ (in-range j))) (r 0 i)))))
   ranges)
  (newline))
#|
(run 5 1000000)
(run 50 100000)
...
(run 50000 100)
|#
(for ((i (in-range 5)))
  (run (* 5 (expt 10 i))
       (expt 10 (- 6 i))))
       
       

