I X Code X 1 wrote:
Your result:
Let me know! lol
Here's my solution and a little test I did:
Code: Select all
(defun generate-list (n)
(labels ((rec (lst n)
(if (> n 0)
(rec (cons (random 10000) lst) (1- n))
lst)))
(rec nil n)))
(defun my-min (lst)
(labels ((rec (lst min)
(cond ((null lst) min)
((< (car lst) min) (rec (cdr lst) (car lst)))
(t (rec (cdr lst) min)))))
(rec lst (car lst))))
I
do like me some tail recursion...
For n=1000
CL-USER> (time (my-min sample-list))
43,008 processor cycles
CL-USER> (time (apply #'min sample-list))
53,184 processor cycles
n=10000
CL-USER> (time (my-min sample-list))
459,144 processor cycles
CL-USER> (time (apply #'min sample-list))
810,024 processor cycles
n=100000
CL-USER> (time (my-min sample-list))
3,511,728 processor cycles
CL-USER> (time (apply #'min sample-list))
36,217,608 processor cycles
The exact numbers vary drastically between runs, but APPLY is clearly slower, and also runs out of stack frames when n = 1000000, so there's nothing wrong with rolling your own solution when you need to. You might refer to your Lisp implementation's own MIN function and see what it does differently to your attempt.
"If you want to improve, be content to be thought foolish and stupid." -Epictetus