ZaneH.
ZaneH.

Reputation: 45

No idea what is wrong with my code Returns 1 when should return 5

(defn counttrue [val]
  (count (filter identity '(val))))

It has something to do with how I'm calling it from the list, of this I'm sure. Because when I run

(count (filter identity '(1 2 3 true true false nil))) 

It works just fine. Some how between defn and calculating I am missing something.

I've also tried running it with #(if % %) '(val) And I get the same answer.

I've seen similar code on this site but nothing that answers this particular question. Am I just calling val wrong?

Upvotes: 0

Views: 65

Answers (1)

Bossie
Bossie

Reputation: 3612

'(val) is a list of exactly one element: the original list. Try:

(defn counttrue [val]
  (count (filter identity val)))

Then:

(counttrue '(1 2 3 true true false nil))

Upvotes: 1

Related Questions