I study Lisp and wrote the following function to collect a list of results.
(defun collect (func args num)
(if (= 0 num)
()
(cons (apply func args)
(collect func args (- num 1)))))
He gave a similar result to the built-in loop function.
CL-USER> (collect
(4 0 3 0 1 4 2 1 0 0)
CL-USER> (loop repeat 10 collect (random 5))
(3 3 4 0 3 2 4 0 0 0)
However, my collection function hits the stack when I try to create a list of 100,000 items long
CL-USER> (length (collect
Control stack guard page temporarily disabled: proceed with caution
While the loop version is not
CL-USER> (length (loop repeat 100000 collect (random 5)))
100000
How can I make my version more space-efficient, are there any consing alternatives? I think the tail is recursive. I am using sbcl. Any help would be great.
source
share