i’m trying redefine the function “range” in Racket.
(define (my-range a b)
(if (> a b)
null
(cons a (my-range (+ 1 a) b))))
;; Test
(my-range 2 5)
;; -> (cons 2 (cons 3 (cons 4 (cons 5 empty))))
Now I want to extend my-range as follows:
(define (my-range a b step) ...)
e.g. (my-range 2 6 1) –> (list 2 3 4 5)
The first number is a and each successive element is generated by adding step to the previous element. The sequence stops before an element that would be greater or equal to b. How can I do this?
From the comments I guess you already found the solution. For completeness’ sake, here it is:
In fact, this procedure is rather common and it can be expressed in several ways. As @dyoo has pointed,
rangeis a standard procedure:Also, in terms of
build-list, another standard Racket procedure:Or using streams: