Hi Rick,

First of all:
#(x) is equivalent with (fn [] (x)). So as an example with reduce:
user=> (reduce #(+ %1 %2) (range 1 101))
5050
user=> (reduce (fn [x y] (+ x y)) (range 1 101))
5050

So it should be obvious, that #(3) throws exception as soon as it is
called, since it is equivalent to (fn [] (3)). So you actually try
to "call" an Integer.

On 23 Okt., 11:29, "R. P. Dillon" <[EMAIL PROTECTED]> wrote:
> Using #() for "one off" functions, you get some odd behavior:
> (reduce #(3) (range 1 100))
> throws an exception:
> java.lang.IllegalArgumentException: Wrong number of args passed to:
> eval--2365$fn
> as does:
> (reduce (fn [] 3) (range 1 100)

This case is different. Without any %, #() retuns function, which
does not expect to be called with any argument. However reduce needs
a function which accepts two arguments:
user=> (doc reduce)
-------------------------
clojure/reduce
([f coll] [f val coll])
  f should be a function of 2 arguments. If val is not supplied,
                           ^^^
  ...

That's also the reason, why you example with fn also fails.
(reduce (fn [a b] 3) (range 1 100)) should work however.

> but:
> (reduce (constantly 3) (range 1 100))

The reason here is, that (constantly 3) is equivalent to
(fn [& args] 3). So the function returned by constantly takes
arbitrary many arguments, in particular two. So reduce is happy.

> performs as one would expect.

The exceptions are also to be expected.

> There are other trivial odd cases:
> (#(3))
> produces an exception.
> But:
> ((fn [] 3))
> does not.
See initial remarks.

> Finally,
> (#(%))
> or even:
> (#(%1))

Again see initial remarks. #(%1) returns a function of one argument
which actually calls this argument. So it is equivalent to
(fn [x] (x)). Doing (#(%)) is missing this argument and you get
an exception.

user=> (#(%) (fn [] (println "Hello")))
Hello

> throws an exception, but:
> ((fn [a] a))
> does not.

This does also throw an exception.

> I'll admit I haven't look at the source for the #() macro, but is this
> type of behavior intended?  I don't fully grok macros in lisp, so I
> may be missing something...

I think you just had a misunderstanding of #(). I hope I could shed
some light on its usage.

Sincerely
Meikel


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to