Thanks for all the suggestions!
This modified version is very close to that in the thread:
http://groups.google.com/group/clojure/browse_thread/thread/f0303a9e00b38529/99f02fef21721a2f?lnk=gst&q=alioth+tree#99f02fef21721a2f
(Thanks for pointing that out Meikel. I should have searched old
threads before posting.)
(defn make-tree [item depth]
(if (zero? depth)
[item nil nil]
(let [item2 (* 2 item) depth-1 (dec depth)]
[item (make-tree (dec item2) depth-1) (make-tree item2
depth-1)])))
; Note: (+ (tree 0) (check-tree (tree 1)) (- (check-tree (tree 2))))
seems to require
; the creation of an intermediate list and runs twice as slow
(defn check-tree [tree]
(if tree
(- (+ (tree 0) (check-tree (tree 1))) (check-tree (tree 2)))
0))
(defn sum-trees [iterations depth]
(let [sum #(+ (check-tree (make-tree % depth))
(check-tree (make-tree (- %) depth)))]
(reduce + (map sum (range 1 (inc iterations))))))
(time (println "result:" (sum-trees 10000 10)))
Running in Clojure REPL for java 1.6.0_11 with -server option:
result: -20000
Elapsed time: 6080.294283 msecs
Wow! Elegant and fast!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---