本文介绍了ClassCastException java.lang.Long 不能转换为 clojure.lang.IFn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,它接受年数和薪水,然后递归地将薪水加倍,直到年数用完.但是,我不断收到此错误:

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:

ClassCastException java.lang.Long 不能转换为 clojure.lang.IFn

代码

(defn calculate-salary
    [years salary]
    (if (= years 0)
        (salary)
        (calculate-salary (- years 1) (* salary 2))))

我对 Clojure 非常陌生,所以我确信它很简单,但我似乎无法弄清楚.

I'm very new to Clojure so I'm sure its something simple, but I just can't seem to figure it out.

推荐答案

这个错误的含义应该不难理清:一个数字被用在了需要函数的地方.

The error's meaning shouldn't be too hard to sort out: a number is being used where a function is expected.

Clojure 中的括号不是分组结构,它们主要用于调用函数调用.如果您将 (salary) 更改为 salary,您将返回该数字,而不是尝试将其作为无参数函数调用.

Parenthesis in Clojure are not a grouping construct, they are used primarily to invoke function calls. If you change (salary) to salary you will return the number rather than attempting to call it as a no-argument function.

这篇关于ClassCastException java.lang.Long 不能转换为 clojure.lang.IFn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:48