java.lang.Boolean cannot be cast to clojure.lang.IFn

536 views
Skip to first unread message

garyk

unread,
May 31, 2010, 8:11:50 AM5/31/10
to Clojure
Hi,
I've started learn Clojure. While I am writing some functions I was
getting the java.lang.Boolean cannot be cast to clojure.lang.IFn. I
was wondering if somebody could help me to move on:

; for testing
(def sudoku ( sorted-map '[1 1] '(98) '[1 2] '(123) '[2 1] '(56789)
'[2 2] '(456)))
(def vall 4)
(def poss (vector [1 1] [1 2]))


(defn valueIn [map value positions]
(loop [start (first positions) end (rest positions) cont false]
(if (or (true? cont) (nil? start))
cont
(recur (cont (contains? (get map start) value)) (start (first
end)) (end (rest end))))))

The call=> (valueIn(sudoku vall poss) gives me the above error.

Thank you,
-- Gary

Joost

unread,
May 31, 2010, 11:14:02 AM5/31/10
to Clojure
On May 31, 2:11 pm, garyk <gkhomin...@gmail.com> wrote:
> Hi,
> I've started learn Clojure. While I am writing some functions I was
> getting the java.lang.Boolean cannot be cast to clojure.lang.IFn. I
> was wondering if somebody could help me to move on:
>
> ; for testing
> (def sudoku ( sorted-map '[1 1] '(98) '[1 2] '(123) '[2 1] '(56789)
> '[2 2] '(456)))
> (def vall 4)
> (def poss (vector [1 1] [1 2]))
>
> (defn valueIn [map value positions]
>   (loop [start (first positions) end (rest positions) cont false]
>     (if (or (true? cont) (nil? start))
>       cont
>       (recur (cont (contains? (get map start) value)) (start (first
> end)) (end (rest end))))))

recur takes positional arguments, not (key value) "pairs" (which
aren't pairs, but function calls).
You want *something* like

(recur (first end) (rest end) (contains? (get map start) value))

but I can't really make heads or tails out of your code. Note that
'(56789) is a list of ONE number, while it looks like you're expecting
it to be a list of digits - that would be '(5 6 7 8 9)

Joost

unread,
May 31, 2010, 11:15:36 AM5/31/10
to Clojure
Oh, and you code throws "clojure.lang.PersistentVector cannot be cast
to java.lang.Number
[Thrown class java.lang.ClassCastException]" for me.

B Smith-Mannschott

unread,
May 31, 2010, 11:27:41 AM5/31/10
to clo...@googlegroups.com
On Mon, May 31, 2010 at 14:11, garyk <gkhom...@gmail.com> wrote:
> Hi,
> I've started learn Clojure. While I am writing some functions I was
> getting the java.lang.Boolean cannot be cast to clojure.lang.IFn. I
> was wondering if somebody could help me to move on:
>
> ; for testing
> (def sudoku ( sorted-map '[1 1] '(98) '[1 2] '(123) '[2 1] '(56789)
> '[2 2] '(456)))
> (def vall 4)
> (def poss (vector [1 1] [1 2]))
>
>
> (defn valueIn [map value positions]
>  (loop [start (first positions) end (rest positions) cont false]
>    (if (or (true? cont) (nil? start))
>      cont
>      (recur (cont (contains? (get map start) value)) (start (first
> end)) (end (rest end))))))

Your (recur ...) is completely mixed up. The specific error you are
seeing is because of the subexpression:

(cont (contains? ...)) which attempts to apply cont as a function to
the result of the call to (contains? ...). cont isn't a function, it's
a boolean value (you gave it the value false at the start of the
loop).

> The call=> (valueIn(sudoku vall poss) gives me the above error.
>
> Thank you,
> -- Gary
>

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

Reply all
Reply to author
Forward
0 new messages