Incorrect 'count' alarm trip on problem #22 #219

Closed
bryndivey opened this Issue Jun 29, 2012 · 3 comments

Comments

Projects
None yet
2 participants

The following code:

(fn [xs]
    (reduce + (for [x xs] 1)))

triggered the 'count' trip alarm on problem number 22.

Contributor

Raynes commented Jun 30, 2012

The problem is that for is a macro and expands to code that uses count. 4clojure's sandbox has to expand macros to look for blacklisted things. It is an unfortunate side effect that for doesn't work here.

Luckily, you can write it in many different ways.

(fn [xs] (reduce + (map (fn [_] 1) xs)))

Is your exact code written with map. There are other ways to solve this problem, but that's the exercise to the reader. :)

@Raynes Raynes closed this Jun 30, 2012

Thanks Raynes, that makes sense - I haven't internalized Clojure's macros' contents yet. My second choice was
(reduce + (map #(1) xs))
which was a bit Pythonesque and didn't work here (no binding for the argument to the lambda, I assume), so it's nice to be pointed out that an anon func with a throw-away would also work.

Contributor

Raynes commented Jun 30, 2012

#(1) doesn't work because it is equivalent to (fn [] (1)).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment