【问题标题】:EmptyList cannot be cast to clojure.lang.IFnEmptyList 不能强制转换为 clojure.lang.IFn
【发布时间】:2016-03-31 01:02:21
【问题描述】:

我想检查给定列表的编号是否为 0,但我的代码返回 EmptyList cannot be cast to clojure.lang.IFn.

(defn fun [ls]
    (println ls)
    (println "hello")
    (println (count ls))
    (if (<= 0 (count (ls))) true
         (println "testing")))

#'user/fun
user=> (fun '())
()
hello
0

ClassCastException clojure.lang.PersistentList$EmptyList cannot be cast to clojure.lang.IFn  user/fun (form-init4069658807942123979.clj:5)

有没有人可以帮帮我?非常感谢!

【问题讨论】:

    标签: clojure


    【解决方案1】:

    这个表达式:

    (count (ls))
    

    应该是

    (count ls)
    

    它试图将ls 的当前值作为函数调用。当 ls 的值是 () 时,它会抱怨试图将空列表作为函数调用,但事实并非如此。

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 写该表达式的“正确”方式是 `(if (seq ls) expression-when-not-empty expression-when-empty)
      猜你喜欢
      • 2015-06-13
      • 2023-04-07
      • 2017-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多