After reading Python's range() analog in Common Lisp, I went thinking that I didn't really like the function interfaces used on the answers.
Three different lambda lists appear there:
(start end &optional (step 1))
: both thestart
andend
arguments are mandatory.(end &key (start 0) (step 1))
: IMHO, using keyword arguments seems overkill for such simple function, and they are there just to hide the fact thatend
andstart
do not appear in the natural order (i.e. firststart
, thenend
)(n &key (start 0) (step 1))
(fromalexandria:iota
): here, the optionality and order of the arguments are right, but at the expense of using a different abstraction.
The thing is that I would like to write (range 6)
to generate (0 1 2 3 4 5)
but also (range 3 6)
to generate (3 4 5)
. And actually, it is easily doable; for instance:
(defun range (start_or_end &optional end (step 1))
(multiple-value-bind (start end)
(if end
(values start_or_end end)
(values 0 start_or_end))
(loop for n from start below end by step collect n)))
But well, I haven't seen this kind of argument fiddling in others code, and as a Lisp newbie I would like to know if that is an acceptable idiom or not.
Update: I have just discovered that Racket provides a range
function similar to the one I was proposing (an also the in-range
generator).