ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn

前端 未结 4 1761
一向
一向 2020-12-30 19:17

I have a function that takes the number of years and salary, then recursively doubles the salary until years is exhausted. However, I keep getting this error:

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-30 19:59

    Since you are new I rewrote your function to be a bit more idiomatic. Also, it uses recur so it will not consume the call stack.

    (defn calculate-salary
      [years salary]
      (if (zero? years)
        salary
        (recur (dec years) (* salary 2))))
    

    Notice the use of the zero? predicate, recur, and dec

    EDIT: typos and grammar

提交回复
热议问题