Actually, I find the structure of the program quite complicated and difficult to read and understand. I think that the reason for this is that the program structure is patterned on the shape
parameter, whose limitation to three elements is, in my opinion, rather arbitrary.
Why do not eliminate such limitation, and make the function more similar to the original numpy one, in which shape
can be any list, even an empty list?
The elimination of this restriction could seem to complicate the problem, but instead it simplifies the function if we use a recursive approach on the structure of shape
:
- if
shape
is empty, then generate a single random number - if
shape
is a list, get the first element, which is a number n, apply n times the function to the rest of the shape and collect the result in a list.
In this way the function is greatly simplified:
(defun randint (&key (start 0) (end (1+ start)) (shape '(1)))
(if (null shape)
(+ start (random end))
(loop as i below (car shape) collect (randint :start start :end end :shape (cdr shape)))))
Finally, I don't like the fact that a function has a key parameter which, if omitted, causes an error (end
). So I would prefer to give it an optional value (for instance start + 1
). Finally, to complete the similarity with the original function, I think we can default the shape parameter to the empty list, so that the call (randint :start n :end m)
produce a single integer in the range specified.