iter-utils.scm 900 B

123456789101112131415161718192021222324252627282930
  1. (library (iter-utils)
  2. ;; export all non-terminal symbols for testing and usage
  3. ;; elsewhere in the code
  4. (export range)
  5. (import
  6. (except (rnrs base) let-values map error)
  7. (only (guile) lambda* λ)))
  8. (define range
  9. (lambda* (start end #:key (end-inclusive #f) (start-inclusive #t) (step 1))
  10. (cond
  11. [(> start end) '()]
  12. [(= start end)
  13. (cond [end-inclusive (cons end '())]
  14. [else '()])]
  15. [else
  16. (cond [start-inclusive
  17. (cons start
  18. (range (+ start step)
  19. end
  20. #:end-inclusive end-inclusive
  21. #:step step))]
  22. [else
  23. (range (+ start step)
  24. end
  25. #:end-inclusive end-inclusive
  26. #:start-inclusive #t ; change after first element
  27. #:step step)])])))