Rich wrote:
> I'm relatively new to Clojure, and I just want to make sure I
> understand things correctly. There are two issues that I'm struggling
> with. Neither is major, but if there is a good solution, I'd like to
> find out.
>
> 1) You must define a method before you call it from another function.
> Stylistically, I prefer to define helper functions after the main
> function, but that seems to cause errors. If I move them in front of
> the main function, the errors go away. I don't know if there's some
> way to declare the methods before you define them (like C header
> files).
>   
The two strategies I've seen before are either to use (declare foo), or 
to put your helpers into another file/namespace, and then :use that 
namespace when you open the primary namespace that will use them.

> 2) To have two or more functions with the same name and a different
> arity, you must define them in a single (defn...).
>
> This crops up when I have a function that was created using a function-
> building macro, and I wanted to define a wrapper with the same name
> that allowed me to enter the arguments in a simpler manner.
>
> Also, it just feels odd. You don't get an error when defining the same-
> named function--but suddenly neither version works. That's a pretty
> serious side-effect. So maybe this is just a bug?
>   
This might make more sense if you understand how namespaces, vars and 
functions work in Clojure.  If you haven't read about those in the 
documentation and/or watched the videos on http://clojure.blip.tv, those 
are good places to start.  In short:

 (defn foo [a] (println "argument a: " a))

is equivalent to:

(def foo (fn [a] (println "argument a: " a)))

So it has to do with the way things are organized.  When you use (def 
...) it will assign a value to a var in the current namespace.  (Which, 
FYI, you can always find by looking at *ns*.)  So you can always assign 
a var using def.  Try it on the repl.  You can (def foo 1) and then (def 
foo 2).  This is why you don't have problems when you (defn foo) 
multiple times.

That at least explains why it works that way.  In general, I think 
rather than trying to adapt a new language and its associated culture to 
your old habits, it is more enjoyable and educational to adapt yourself 
to the new culture.  Try looking through core.clj in the clojure source 
and reading over stuff in clojure-contrib.  When I'm wondering how to do 
something or what is typical in Clojure land the first thing I do is 
grep around in those two places, then google, then ask on IRC or the 
mailing list.  It seems to work.

Cheers,
Jeff

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to