From a5550a69da805f10c5b79f6cab9fb9995beb28bc Mon Sep 17 00:00:00 2001 From: Jens Axel Soegaard Date: Mon, 23 Apr 2007 19:43:43 +0000 Subject: [PATCH] Fixed PR 7650. iota now uses (+ start (* n step)) to calculate the elements, instead of successive subtractions. svn: r6025 --- collects/srfi/1/cons.ss | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/collects/srfi/1/cons.ss b/collects/srfi/1/cons.ss index a16bc4684c..06ad5c4672 100644 --- a/collects/srfi/1/cons.ss +++ b/collects/srfi/1/cons.ss @@ -98,15 +98,18 @@ ;; IOTA count [start step] (start start+step ... start+(count-1)*step) (define iota - (opt-lambda (count [start 0] [step 1]) - (check-arg integer? count 'iota) - (check-arg number? start 'iota) - (check-arg number? step 'iota) - (let ((last-val (+ start (* (- count 1) step)))) - (do ((count count (- count 1)) - (val last-val (- val step)) - (ans '() (cons val ans))) - ((<= count 0) ans))))) + (opt-lambda (count [start 0] [step 1]) + (check-arg integer? count 'iota) + (check-arg number? start 'iota) + (check-arg number? step 'iota) + (unless (or (zero? count) (positive? count)) + (error 'iota "count expected to be non-negative, got: ~a" count)) + (let loop ([n 0]) + (cond + [(= n count) '()] + [else (cons (+ start (* n step)) + (loop (add1 n)))])))) + )