thanks -sun
On Jan 23, 1:43 pm, vthakr <vth...@gmail.com> wrote: > Hi Wubble, > > Looking at the code you have above I thought I might point out that > rather than create an anonymous function inside of sum and then call > it immediately after finishing its description, you could just use the > loop/recur construct which is much more idiomatic clojure code. If you > decide to go that route, your code would look like the following: > > (defn sum [& more] > (loop [total 0 other more] > (if other > (recur (+ total (first other)) (rest other)) > total))) > > loop essentially works the same as the anonymous function you've got > in your sum function, but it allows you to set the parameters when you > define the loop just as you would in a let. Then when you call recur, > the code within the loop is called again, but this time with the > bindings that you supply to the recur statement. This just shortens > the code you wrote and makes it a bit more understandable. Of course, > Jason's suggestions above of using the apply method or 'folding' the > list with the reduce command is the most straightforward (and, I > assume, most performant) method for computing the sum, outside of just > using the '+' function directly of course ;-) > > To execute the function above you could either use (sum 1 2 3 4 5) > or---if you are absolutely dead set on passing a vector to the > function---you could use (apply sum [1 2 3 4 5]). > > Hope that helps. > > Christopher --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---