On Saturday, December 1, 2018 at 8:57:32 PM UTC-5, John Cowan wrote:
> Therefore, at each step of the recursion, (apply plus xs) makes a shallow copy of xs. This O(n) behavior is performed n times, making the procedure quadratic.
In each Scheme there's usually a bound on the number of arguments you can pass to a function, so on the size of the lists that you can "apply" a function to, so the "quadratic" time problem is actually bounded, e.g. (in Gambit):
> (compile-file "sum")
"/tmp/mozilla_lucier0/sum.o1"
> (load "sum")
"/tmp/mozilla_lucier0/sum.o1"
> (define 100i (make-list 100 1))
> (define 1000i (make-list 1000 1))
> (time (apply plus 100i))
(time (apply plus 100i))
0.000179 secs real time
0.000182 secs cpu time (0.000109 user, 0.000073 system)
no collections
237600 bytes allocated
32 minor faults
no major faults
100
> (time (apply plus 1000i))
(time (apply plus 1000i))
0.010328 secs real time
0.010331 secs cpu time (0.002577 user, 0.007754 system)
no collections
23976000 bytes allocated
2968 minor faults
no major faults
1000
> (define 10000i (make-list 10000 1))
> (time (apply plus 10000i))
*** ERROR IN ##exec-stats -- Number of arguments exceeds implementation limit
(plus 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...)
So you can't really apply this code for large lists.
Brad