Reputation: 25
I have a list of values and I am performing some calculation on the first value of list and storing the result in a data structure.
I am new to clojure and I think problem lies in running the loops
(defn cal-funct [mean-value ,st-dev ,values]
(def pi 3.14159)
(def e 2.71828)
(loop [f []
values values
]
(if (not-empty values)
(do
(println str "first " (first values))
(let [y (* (/ 1 (* st-dev (Math/sqrt (* 2 pi)))) (Math/pow e (* -1 (/ (Math/pow (- (first values) mean-value) 2) (* 2 (Math/pow st-dev 2))))))
]
(println str "y is" y)
(recur (rest values) (conj f y))
)
)
f
)
)
)
y should be calculated for all values of #values while its running infinite..
Upvotes: 0
Views: 73
Reputation: 51561
There are several problems with your code. The first is that it is not formatted at all. Clojure has a fairly universally accepted style guide with only few variations, e. g. documented here. Not formatting code leads to incredibly hard to see, but in essence very simple, bugs.
Here is your code, formatted:
(defn cal-funct [mean-value ,st-dev ,values]
(def pi 3.14159)
(def e 2.71828)
(loop [f []
values values]
(if (not-empty values)
(do
(println str "first " (first values))
(let [y (* (/ 1
(* st-dev
(Math/sqrt (* 2 pi))))
(Math/pow e
(* -1
(/ (Math/pow (- (first values) mean-value)
2)
(* 2 (Math/pow st-dev 2))))))]
(println str "y is" y)
(recur (rest values)
(conj f y))))
f)))
In no particular order:
def
in a function body, use let
instead. In this case, it seems that you want pi
and e
to be global constants, so define them outside.,
is whitespace. It serves no purpose in a parameter list like that.str
is a function. I do not think that you want to print it.recur
arguments is not the same as in the loop
bindings.Some more improvements:
(/ 1 whatever)
can be expressed as (/ whatever)
.(* -1 whatever)
can be expressed as (- whatever)
.y
.mapv
.Upvotes: 0
Reputation: 6666
You have your arguments to recur
the wrong way round. You're binding (rest values)
to f
and (conj f y)
to values
in the recur
call, so values
will never be empty.
Upvotes: 3