Skip to content

Instantly share code, notes, and snippets.

@zaltoprofen
Created October 10, 2013 15:52
Show Gist options
  • Save zaltoprofen/6920704 to your computer and use it in GitHub Desktop.
Save zaltoprofen/6920704 to your computer and use it in GitHub Desktop.
(define (range start . args)
(let-optionals* args ((stop #f) (step 1))
(define (rangei start stop ls)
(if (or (and (> step 0) (>= start stop)) (and (< step 0) (<= start stop)))
ls (rangei (+ start step) stop (cons start ls))))
(cond ((= step 0) (error "Step cannot take 0"))
((not (integer? start)) (error "Integer required for 1st argument, but got:" start))
((and stop (not (integer? stop))) (error "Integer required for 2nd argument, but got:" stop))
((not (integer? step)) (error "Integer required for 3rd argument, but got:" step))
((not stop) (reverse (rangei 0 start '())))
(else (reverse (rangei start stop '())))
)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment