News for hackers. Stuff that matters.
Re:Introduction to Clojure by dreish on Saturday October 17, 2009 @10:30AM (#886954)
Don't arrange your closing parens the way the author of this article has unless you want to look like a clueless noob.

Edit: There are other style problems with this article. Worst is probably this one:

  Bad: (map (fn [x] (fibonacci x)) [1 2 3 5 6 7 8 9 10])
  Good: (map fibonacci (range 1 11))
Re:Introduction to Clojure by alrex021 on Saturday October 17, 2009 @12:14PM (#887088)
Absolutely right. Don't arrange your parens to fall separately like author does on new lines. Let the parens flow after one another.

The way your parens should flow:

  (def factorial
    (fn [n]
      (loop [cnt n acc 1]
        (if (zero? cnt)
            acc
          (recur (dec cnt) (* acc cnt))))))
The author doesn't take an idiomatic approach to programming in Clojure in most of his code snippets.

[edit]

Throwing java.lang.Exception if a condition is not meat in your clojure functions? Don't do this either!!!

Re:Introduction to Clojure by jacquesm on Saturday October 17, 2009 @02:14PM (#887208)
> Throwing java.lang.Exception if a condition is not meat in your clojure functions? Don't do this either!!!

What's the proper way of doing that in clojure ?

Re:Introduction to Clojure by alrex021 on Saturday October 17, 2009 @03:17PM (#887304)
-way- is not the problem here. The java.lang.Exception is. :-) Throw specialized "Runtime Exceptions" would be the way to go. For example throw IllegalArgumentException runtime exception if function call receives inappropriate arguments.

You can also define your own specific (app-level) exception by proxying the RuntimeException or other relevant using the clojure's proxy macro.

Re:Introduction to Clojure by jacquesm on Saturday October 17, 2009 @03:29PM (#887318)
Ah right! Apologies, I took it to mean that there was a better way of throwing an exception, not that particular one.

Of course, that makes perfect sense.

Re:Introduction to Clojure by jacquesm on Saturday October 17, 2009 @10:40AM (#886967)
Interesting, since it is actually in some ways easier to see which closing paren goes with which opening one.

My 'C' parser keeps telling me 'wrong bracket' though :)

Re:Introduction to Clojure by dreish on Saturday October 17, 2009 @10:42AM (#886971)
Yes, which is why it is a newbie signal. A Lisp programmer with more than about three weeks of experience doesn't look at parentheses or try to match them up with each other. You know the function has ended because the next line of code has no indentation.
Re:Introduction to Clojure by jacquesm on Saturday October 17, 2009 @10:51AM (#886983)
which suggests the use of a 'close all open parens' token to cut down on all the )))))) stuff at the end of a line, which can get really tedious, especially when editing in 'dumb' editors.
Re:Introduction to Clojure by dreish on Saturday October 17, 2009 @10:56AM (#886986)
That's not much help when you want to close all but one or two.

The solution is to use the right tool for the job. Emacs has something called paredit-mode that relieves you of having to type the closing parens.

Re:Introduction to Clojure by gruseom on Saturday October 17, 2009 @11:35AM (#887038)
paredit-mode

Yes! I can't do without it now. BTW somebody told me Paredit is a port of the old Interlisp structured editor.

There's also http://news.ycombinator.com/item?id=877386, if you haven't seen it.

Re:Introduction to Clojure by dreish on Saturday October 17, 2009 @12:19PM (#887094)
I'm glad I took the plunge, too. At first, it seemed like a really bad idea -- far too awkward and unfamiliar for the minor advantage of automatically inserting closing parens. I kept at it mainly out of an attraction to the novel and quirky for long enough to discover that its biggest advantages aren't from not having to type ")" -- they're for things like barf, slurp, splice, and the S-expression-aware kill-line.

Whenever I'm programming in a non-Lisp language, I really miss those kinds of editor features that are enabled by having an idiot-simple syntax.

Re:Introduction to Clojure by jacquesm on Saturday October 17, 2009 @11:07AM (#886999)
Someone should make a really good course to 'bridge' from being an experienced PHP, Perl, Java or Python programmer to that same level of competence using Clojure.
Re:Introduction to Clojure by rw on Saturday October 17, 2009 @04:03PM (#887350)
From my experience, the syntactic gap is wider than the semantic one. I suggest first, perhaps, translating Python into faux S-exprs (should be easy, given the indentation style). From there one might emphasize declarative vs. imperative, and functional as a subset of the former.
Re:Introduction to Clojure by mahmud on Saturday October 17, 2009 @12:27PM (#887106)
Clojure has the highest percentage of "noob" users of any language. It's just 2 years old and everybody is pretty much wetting their feet with it just now.
Re:Introduction to Clojure by alrex021 on Saturday October 17, 2009 @12:32PM (#887110)
Very true. But then the author of the article needs to get his feet a lot more wet because its plain and simply bad code.
Re:Introduction to Clojure by cema on Saturday October 17, 2009 @02:53PM (#887262)
This is true, but the style of the code layout follows Lisp tradition, which may be more than 50 years old! :-)