Im getting the dreaded * cannot be cast to IFn error with a simple mathematical function here:
(defn calc [sc1 m1 alpha beta gamma ta1 tb1 min_m1 min_tca tca_ratio x32]
(*
(max(0,
(+
(* alpha log(sc1))
(* beta log(m1))
(* gamma (/ ta1 tb1))
(- log(ta1) log(tb1)))))
(max(x32,(/ m1 min_m1)))
(max(x32,(/ tca_ratio min_tca)))))
;;;;;;;;;;;;
The args are simply a bunch of numbers :
(calc 1 2 3 4 5 1 2 3 4 5 1)
My thoughts / My Question
Usually, when I get this error, I find it is due to
1) An extra parenthesis (i.e. when I've accidentally put an extra closure in my code) OR
2) Arguments that are misordered (obviously - a cast exception) .
My question is simply... how to fix this snippet, and optionally -- how can I defeat this common exception once and for all ? It seems that it occurs quite often in my Clojure coding expeditions, and I'm thinking maybe I still haven't got the right development style yet.
-------------------------------------------------------
UPDATE :
I've riddled my code with unregular syntax. The errors were in the internal functions, which used an java/c style function calls : for example max / log ...
Looks to me like the offending expressions are (x32,(/ m1 min_m1)) and (x32,(/ tca_ratio min_tca)) and also the log(..) ones.
Clojure follows the standard lisp convention of having the first element of an unquoted list be a function to call. In other words, you want to have (log ...) instead of log(...) and (max ...) instead of (max (...))
Catching these errors is pretty easy in your particular style: everything that matches "XXX(" where XXX is any number of non-space, non-parenthesis characters is an error.
It looks to me like you have a problem with the log(sc1)
, etc. calls.
Ought to look (assuming log is defined somewhere) something like:
(defn calc [sc1 m1 alpha beta gamma ta1 tb1 min_m1 min_tca tca_ratio x32]
(*
(max 0
(+
(* alpha (log sc1))
(* beta (log m1))
(* gamma (/ ta1 tb1))
(- (log ta1) (log tb1))))
(max x32 (/ m1 min_m1))
(max x32 (/ tca_ratio min_tca))))
If you just want to use the Java log, replace log
with Math/log
which calls the static log function in java.lang.Math
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With