how to make a range clojure
To create a range in Clojure, you can use the range
function. The range
function generates a sequence of numbers from a starting value (inclusive) to an ending value (exclusive), with an optional step size.
Here's the syntax for using the range
function:
(range start end step)
start
: The starting value of the range (inclusive).end
: The ending value of the range (exclusive).step
: The step size between each value in the range (optional).
Here are a few examples:
- Creating a range from 0 to 9 (exclusive):
(range 0 10)
Output: (0 1 2 3 4 5 6 7 8 9)
- Creating a range from 1 to 10 (exclusive) with a step size of 2:
(range 1 11 2)
Output: (1 3 5 7 9)
- Creating a range from 10 to 1 (exclusive) with a negative step size of -1:
(range 10 0 -1)
Output: (10 9 8 7 6 5 4 3 2 1)
Please note that the range
function returns a lazy sequence, which means the values are computed on-demand. If you need to use the range as a concrete collection, you can wrap it with the vec
function to convert it into a vector.
I hope this helps! Let me know if you have any further questions.