And in cases like this recursive function, not only you need to do as above
said, but also you will want to make sure that the recursive calls use the
memoized function, otherwise, only "top level" results are getting the
benefit of memoization, resulting in the same tragic exponential time
behaviour. In this specific fib example, only "top level" memoization would
be basically useless:
user=> (time (fib-memo 30))
"Elapsed time: 94.33 msecs"
user=> (time (fib-memo 30))
"Elapsed time: 0.059 msecs"
user=> (time (fib-memo 40))
"Elapsed time: 11512.249 msecs"
You will want something like:
(def fibmem3
(memoize
#(condp = %
0 0
1 1
(+ (fibmem3 (- % 1)) (fibmem3 (- % 2))))))
user=> (time (fibmem3 30))
"Elapsed time: 1.643 msecs"
832040
user=> (time (fibmem3 30))
"Elapsed time: 0.063 msecs"
832040
user=> (time (fibmem3 40))
"Elapsed time: 0.562 msecs"
102334155
user=> (time (fibmem3 40))
"Elapsed time: 0.062 msecs"
Regards,
Paulo
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.