4 ms·
But a Lisper would do something along the lines of (defun list-sum (lyst &optional (acc 0)) (if (eql 0 (length lyst)) acc (lis
by thegeomaster 12y ago
But a Lisper would do something along the lines of
(defun list-sum (lyst &optional (acc 0))
(if (eql 0 (length lyst))
acc
(list-sum (cdr lyst) (+ acc (car lyst)))))
And any respectable compiler (with TCO) would compile it to a plain old loop. In this case, I'm puzzled because the author implemented this solution knowing that it is going to be compiled to a slow recursive routine.
- adwf 12y agoI'd hope they'd also first think of: (reduce #'+ list-of-numbers) Then using a library like lparallel [1], you can convert it to the parallel version easily: (preduce '+ list-of-numbers) [1] http://lparallel.org/preduce/ http://lparallel.org/preduce/