I found a simple(but inefficient) solution to a problem which basically can be written like this in Clojure:
(defn make-someof [data]
(fn sumf
([x n ds]
(if (= n 1)
(if (some #{x} ds) 1 0)
(reduce + (for [m (filter #(< % n) ds)]
(sumf (- x m) (dec n) (disj ds m))))))
([x n] (sumf x n data))))
Which could be called like this:
((make-someof #{1 2 3 4 5 6}) 7 2)
But as it turns out, the form in the reduce section of the make-sumof
function gets evaluated only once. (i.e. the filter
form which has 6 values in it get iterated only for it's first value!)
I also tried using doall
on the for
form, thus forcing it to get computed, but the moment recursive call reaches the first form (when condition (= n 1)
is held) the resulting expression (in this case 1 in the (if (some #{x} ds) 1 0)
form) is returned as the result of the whole invocation chain.
It just doesn't make sense to me.
Maybe I'm making a big mistake or it's a bug in Clojure?
Note: This function is supposed to count the number of ways which the number 7 could be written as the sum of 2 other numbers from specific distinct numbers (in this case #{1 2 3 4 5 6}
).
I'm rather interested in figuring out what's happening here, as this function could be written with a loop
form.