This pertains to question #15 on 4clojure “Double Down.” Original problem link here: https://www.4clojure.com/problem/15
My goal is to evaluate: “Do all the statements evaluate to true?” If yes, return true, else return false. In short, I want to write a single expression that will run all the test cases at once.
Ps. I have written this question post backwards on purpose because I think question 3 is the best question, but for those who want to see my thought process in more detail, they can work their way to 3 from 1 or 2. Sorry and thank you
Is it possible to tweak “example B” so that it works as desired? If so, how?
For what cases is
every? true? appropriate, and what cases is
every-pred true? appropriate? I have a hunch that, semantically speaking,
every? takes one predicate to evaluate over a collection of values (ie. non-predicates), whereas,
every-pred true? takes many predicates and simply asks. I think my brain has been scrambled…
Why does this work as expected…
;; example A (and (= (#(* % 2) 2) 4) (= (#(* % 2) 3) 6) (= (#(* % 2) 11) 22) (= (#(* % 2) 7) 14)) ;; => true
… whereas the following simply returns an error:
;; example B (every? true? ((= (#(* % 2) 2) 4) (= (#(* % 2) 3) 6) (= (#(* % 2) 11) 22) (= (#(* % 2) 7) 14))) ;; => class java.lang.Boolean cannot be cast to class clojure.lang.IFn
… Upon some further reflection and research, I found this:
;; example C ((every-pred true?) (= (#(* % 2) 2) 4) (= (#(* % 2) 3) 6) (= (#(* % 2) 11) 22) (= (#(* % 2) 7) 14)) ;; => true
… which appears to work as desired
In short, I’d like to understand why “example B” gives an error, and if there was a better (clearer or simpler) way to solve for my personal challenge than examples A and C.