Hi,
On Oct 12, 4:34 am, gL <[email protected]> wrote:
> (use 'clojure.contrib.combinatorics)
>
> (defn run-euler-024 []
> (binding [max 0]
> (let [perms (take 1000000 (lex-permutations [0 1 2 3 4 5 6 7 8
> 9]))]
> (doseq [perm perms]
> (set! max perm)))
> max))
>
> The solution only works with a var name that equals to a Clojure name
> (here "max").
>
> Changing "max" to "res" results in java.lang.Exception: Unable to
> resolve var: res in this context
If you feel to write code like this, then please remember reduce! Here
some random example.
(defn max
"Returns the maximum of a sequence of natural numbers."
[a-seq]
(reduce (fn [max elem] (if (< max elem) elem max)) 0 a-seq))
Instead of the more imperative style like the following.
(defn max
"Returns the maximum of a sequence of natural numbers."
[a-seq]
(let [max (atom 0)]
(doseq [elem a-seq]
(when (< @max elem)
(reset! max elem)))
@max))
(This is in principle what you tried to achieve with your binding.)
Ugh. Please don't write such code.
Sincerely
Meikel
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---