On 02.06.2009, at 18:00, Wilson MacGyver wrote:
> actually I had the exact same reaction. So I'd echo Andrew's comment.
> Is this different than pattern-matching in say haskell/scala?
The difference is that a pattern match can fail, and in that case
other patterns are tried. Clojure's destructuring assumes that the
value has the right structure. If it doesn't, then you will get an
exception thrown.
On 02.06.2009, at 18:02, Wilson MacGyver wrote:
> I saw that clojure has loop. But in other functional languages,
> using loops are always discouraged. So I didn't know if loop
> was the "clojure" idiomatic way of doing this.
Loops in Clojure are purely functional. They are in fact equivalent
to an embedded recursive function. Unlike the typical use of loops
in, say, Java, nothing is changed inside a loop, so there are no side
effects.
Here's your example with the loop rewritten as an explicit recursive
function:
(let [size 10
loop-fn (fn [max]
(if (or (>= (/ max (Math/log max)) size)
(>= max Integer/MAX_VALUE)
(<= max 0))
max
(recur (* 2 max))))]
(loop-fn 2))
As you can see, the differences are cosmetic. There is no need to
have a bad functional conscience for using loops!
Konrad.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---