Clojure: use binding in cond to place recur only in tail position?

I have a code that currently gives me an error, because recur can only be in the tail position. here is the function:

(defmethod transposer
    Long [scale degree]
    (loop [new-scale scale count degree]
    (cond 
        (zero? count) new-scale 
        (< count 0) (recur (step (reverse new-scale)) (inc count)))
        :else (recur (step new-scale) (dec count))))

One way I can think of is to fix this conditional obligation if I could say: when count is less than zero, set the count-operator to "inc", otherwise set to "dec", and then go back to the end.

Then it will fix my problem. But I'm not sure how to do this in clojure, or if possible, when-let and if-let don't seem to do this. What is the best way to fix my code to use only one recur?

EDIT: A few things I learned here:

1) "recur" defn, . , , recur use loop/recur, , . , .

2) , , cond , . .

3) - , "let" . java, clojure .

+3
2
(defn foo [scale degree]
  (loop [new-scale scale count degree]
    (cond 
      (zero? count) new-scale 
      (< count 0) (recur (step (reverse new-scale)) (inc count))
      :else (recur (step new-scale) (dec count)))))

, , ( , defn ).

recur - , , , .

parens ( count). , ( -indent ). ( la clojure intellij, , ).

update: loop?

(defn foo [scale degree]
  (cond 
    (zero? degree) scale 
    (< degree 0) (recur (step (reverse scale)) (inc degree))
    :else (recur (step scale) (dec degree))))
+4

: " , , - , : , count-operator " inc ", " dec ", ." :

(let [d (if (neg? degree) inc dec)]
  (recur (step scale) (d degree)))

, , , . :

(let [[s d] (if (neg? degree) [reverse inc] [identity dec])]
  (recur (step (s scale)) (d degree)))

, , cond recur "" ( ) .

+3

All Articles