This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
1) How the "return" mechanism is working here ? and
2) How the syntax of 'catch IllegalStateException e "nnnnn"' is actually working ? Its kind of strange because, I can't tell wether the catch is "pure" clojure, or just syntactic sugar that is wrapping the java exception handling syntax.
In Clojure, everything is an expression that yields a value - there are no "statements".
Therefore a try/catch form will yield a value. Either the value of the try part (if that succeeds) or the value of the catch part (if an exception is caught). You can also specify a finally part to do any resource cleanup but the value of that part is ignored (in favor of the try value or the catch value).
Strictly speaking the try part allows for a sequence of expressions, just like do, let, function bodies and a few other forms. The value returned is the value of the last expression. Similarly for the catch part, which starts with an exception class and a var that is bound to the value of the caught exception (so you can access the exception details in the catch part). In that respect, it's just like the Java counterpart:
The equivalent Clojure:
Does that help?
Joined: Aug 30, 2005
yes. meanwhile, however I see you mentioned "let"..... Im not quite sure how "let" is working. I'm used to the python "let", which can be used to incrementally "offer" some output to the calling method. When I see "let" used in clojure, however, I fail to see the parallel.
Think of let as a block that introduces local variables:
This introduces a series of bindings of names to values, which are then available only inside the lexical scope of the let form. In Scala you might have:
or in Java:
Hope that helps explain let?
Joined: Aug 30, 2005
Okay... your being so helpful i figured id shoot another one out there ....
1) ive seen alot of code with "if" and "do". However, not much "if " "else" . Is the "if/else" paradigm an unclojurely concept ?
2) Why is it that let is nested ? for exmple, in the below function, there is an "if" after "let".... Does that mean that the "if" function is an "argument" to let ???
The if form can have two or three parts: the conditional, an expression to evaluate if truthy and an optional expression to evaluate otherwise (if falsey):
The reason you may see if-do is because if can only take a single expression for the truthy situation (and a single expression for the falsey situation). If it's just if and multiple expressions, you'll most likely see when instead:
That's equivalent to the following expression where the do form lets you evaluate multiple expressions and just return the last one:
In your code example, let just introduces a local binding for past-visitor that is available in the body of the let (the if form). Your if has a condition, past-visitor, an expression to evaluate if truthy (returning the Welcome back string) and an expression to evaluate if falsey (a do form with two expressions, updating the ref visitors and then returning the Hello string).
On a point of style, Lisp / Clojure developers do not use that trailing brace style. You're going to see this instead:
Since most editors used by Lisp / Clojure developers automatically match parens, folks tend to work with stacked parens rather than the "matched brace" style that you usually see in C-style languages like Java. The stacked parens style, where structure is clearly shown with indentation, also makes it clear that the nested if has three expressions and therefore has an "else" expression.