java - Clojure : java. lang.Character 无法转换为 clojure.lang.IFn

标签 java clojure lisp

我只是想用 Clojure 写一个简单的小猜谜游戏,我得到了这个错误。我看不到我的 Character 在哪里被视为函数,因为结果输入也预测不应该存在这样的问题。这是代码:

(ns clojure.examples.hello
  (:gen-class))

(ns clojure-noob.core)

(defn takeFst [x n]
    (if (= n 0) () (cons (first x) (takeFst (rest x) (- n 1))))
)

(defn createSeq [elem n]
    (if (= n 0) () (cons elem (createSeq elem (- n 1))))
)

(defn fnEquals? [n]
    (fn [elem] (= elem n))
)

(defn removeEach [x elem]
    (remove (fnEquals? elem) x)
)

(defn containsString? [s ch]
    (not (empty? (filter true? (map = (createSeq ch (count s)) s))))
)

(defn akasztofa! [s lives]
    (println s)
    (if (and (not= () s) (not= lives 0))
        (
            (def guess (eval (read)))
            (if (containsString? s guess) (akasztofa! (removeEach s guess) lives) (akasztofa! s (- lives 1)))
        )
        ()
    )
)

(akasztofa! "hab" 10)

我得到的输出是这样的:

hab
(a b)
(b)
() 
Exception in thread "main" java.lang.ClassCastException: 
java.lang.Character cannot be cast to clojure.lang.IFn, compiling:
(/home/cicaharcos/Documents/Clojure/First/akasztofa/main.clj:38:1)

我的输入是:\h\a\b

最佳答案

错误来自尝试将字符作为函数求值,例如:

(\a) => Exception in thread "main" java.lang.ClassCastException: java.base/java.lang.Character cannot be cast to clojure.lang.IFn, 

我认为是 if 语句中的额外括号试图将字符作为函数求值。请记住,在 Clojure 中,括号不像在 Java 中那样“分组”,它们的意思是“函数调用”。

您的代码还有一些其他问题,值得注意的是对空列表使用 ()。您必须像这样引用列表:

'()

或者,更好的是,使用带方括号的空 vector (不需要引号):

 []

如果您使代码看起来像下面这样,它似乎可以工作:

(ns clojure.examples.hello
  (:gen-class))

(ns clojure-noob.core)

(defn takeFst [x n]
    (if (= n 0) [] (cons (first x) (takeFst (rest x) (- n 1))))
)

(defn createSeq [elem n]
    (if (= n 0) [] (cons elem (createSeq elem (- n 1))))
)

(defn fnEquals? [n]
    (fn [elem] (= elem n))
)

(defn removeEach [x elem]
    (remove (fnEquals? elem) x)
)

(defn containsString? [s ch]
    (not (empty? (filter true? (map = (createSeq ch (count s)) s))))
)

(defn akasztofa! [s lives]
    (println s)
    (if (and (not= [] s) (not= lives 0))
        (let [guess (eval (read))]
          (if (containsString? s guess)
            (akasztofa! (removeEach s guess) lives)
            (akasztofa! s (- lives 1))))
        [] ))

(akasztofa! "hab" 10)

结果:

hab
\h      ; <= user input plus <ret>
(a b)
\a      ; <= user input plus <ret>
(b)
\b      ; <= user input plus <ret>
()

清空列表:

使用count函数,可以看出问题:

demo.core=> (count ())
0

demo.core=> (count (\b))

ClassCastException java.base/java.lang.Character cannot be cast to clojure.lang.IFn  demo.core/eval16682 (form-init2403719904611886388.clj:1)

demo.core=> (count (1))

ClassCastException java.base/java.lang.Long cannot be cast to clojure.lang.IFn  demo.core/eval16686 (form-init2403719904611886388.clj:1)

demo.core=> (count '(\b))
1

所以你可以使用 () (未加引号)作为一个空列表(我忘记了),但是如果它是非空的,它会失败,除非你引用它。使用 vector 更简单且不易出错:

demo.core=> (count [])
0
demo.core=> (count [\b])
1
demo.core=> (count [1])
1

关于java - Clojure : java. lang.Character 无法转换为 clojure.lang.IFn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47858857/

相关文章:

lisp - 从 Lisp 中的递归返回 NIL

java - Force MigLayout shrink like GridBagLayout for hidden Objects

java - java中GUI设计的问题

map - 去雾化 map

clojure - 测试返回 bool 值的函数

lisp - X 秒后中止函数 - lisp

java - 什么是NullPointerException,我该如何解决?

java - 如何使用 Openapi Generator 从 Swagger yaml 生成 SpringBoot 模型

clojure - 使用 Overtone 从磁盘读取声音文件?

macros - 初始内容由函数确定的数组