0
votes

In a riddle I have to complete the following expressions in order to evaluate to true. There must be one single insertion, which fits to all of them.

(= 15 (reduce __ [1 2 3 4 5]))
(=  0 (reduce __ []))
(=  6 (reduce __ 1 [2 3]))

The third expression provides a start value. Hence my replacement cannot provide another one.

This function would pass the first and the third truth test:

#(+ %1 %2)

However, the second expression yields the following error:

clojure.lang.ArityException: Wrong number of args (0) passed to (...my function id)

It looks like usually reduce calls the given function only if the length of start value + collection is bigger than 2. If this length is 0, like in the case above, it is called as well - with 0 arguments.

Does anyone have a hint how to carry on here?

1
hint : what is the result of (+) ? - nha
thanks! of course, it could not be much simpler! (my solution in the meantime was: (fn [& more] (if (= (count more) 0) 0 (apply + more))) ha, ha) - Anton Harald
Glad it helps, you can still simplify it, a lot ;) Don't hesitate to look at other solutions if you are using 4clojure. - nha
Isn't (+) a tie fighter? - Mark Fisher
i tried to answer my question here in stackoverflow, but there is a min of 30 characters. too bad. no chance with one char. - Anton Harald

1 Answers

0
votes

From the comments, the solution is clearly +, but maybe it's valuable to look at it in some detail to see why. As it turns out, there's a lot to it.

First, let's look at reduce to see what the requirements are:

(defn reduce
  "f should be a function of 2 arguments. If val is not supplied,
  returns the result of applying f to the first 2 items in coll, then
  applying f to that result and the 3rd item, etc. If coll contains no
  items, f must accept no arguments as well, and reduce returns the
  result of calling f with no arguments.  ..."
  ... 
  ([f coll]
     (if (instance? clojure.lang.IReduce coll)
       (.reduce ... coll f)
       ...))
  ([f val coll]
     (if (instance? clojure.lang.IReduceInit coll)
       (.reduce ... coll f val)
       ...)))

This is a multi-arity function that either takes a function and a collection, or a function, initial value and a collection.

+ is also a multi-arity function that behaves depending on how many arguments you pass to it. The source below (edited for the parts we care about), shows reduce is satisfied by 0-arity and 2-arity.

(defn +
  "Returns the sum of nums. (+) returns 0..."
  ...
  ([] 0)
  ...
  ([x y] (. clojure.lang.Numbers (add x y)))
  ...

Clearly (reduce + []) calls the first clause and returns 0. Test 2 is explained.

This works for the first test by applying the add function to each pair of Numbers, which happens in the Java internals for Clojure, in a tight for loop.

The final test works exactly like the first, except it calls the [v val coll] implementation of reduce. This applies a slightly different internal function, but with the same effect.

Notes

[1]: IFn is the Java interface for clojure functions