EmptyList cannot be cast to clojure.lang.IFn - clojure

I want to check whether the number of a given list is 0 or not, but my code returns 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)
Is there any one can help me out? Thank you so much!

this expression:
(count (ls))
should be
(count ls)
It's trying to call the current value of ls as a function. When ls is the value () then it complains about trying to call the empty list as a function, which it is not.

Comment: Please have a look at empty? - clojure.core | ClojureDocs and What is the correct "clojure way" to check if a collection is non empty - Stack Overflow.

Related

Tracking down a StackOverflow in a Clojure program, contains SSCCE

I am having a hard time tracking down a stackoverflow error produced by my clojure implementation of the Bron-Kerbosch algorithm.
Below is my implementation of the algorithm itself, and here is a link to a SSCCE outlining the issue http://pastebin.com/gtpTT4B4.
(defn Bron-Kerbosch [r p x graph cliques]
;(println (str "R:" r " P:" p " X:" x " Clq:" cliques))
(cond (and (empty? p) (empty? x)) (concat cliques r)
:else
(let [neigh (neighV graph (dec (count p)))]
(loop [loop-clq '(cliques)
loop-cnt (dec (count p))
loop-p '(p)
loop-x '(x)]
(cond (= -1 loop-cnt) loop-clq
:else
(recur (concat loop-clq
(Bron-Kerbosch (concat r loop-cnt)
(concat p neigh)
(filter (set x) neigh)
graph cliques))
(dec loop-cnt)
(filter (set p) loop-cnt)
(concat x loop-cnt)))))))
I would have to assume that the issue obviously lies within one of my two bootstrap conditions (cond (and (empty? p) (empty? x)) and (cond (= -1 loop-cnt) because the function algorithm is recursive.
Though this assumes that I am building the lists x r p correctly. Judging by the output of the commented out print statement (cliques is always printed as an EmptyList) I assume that my list comprehension might also be the issue.
Along the same lines, the other issue I can somewhat see is that I am not actually calling the algorithm properly in the BK-Call function (in the SSCCEE).
My overall question is what is causing this? Though this is somewhat too open, another question that might lead me to my answer is how I might go about using the print statement on the first line.
When the print statement is uncommented it produces the output
R:clojure.lang.LazySeq#2044e0b9 P:clojure.lang.LazySeq#7f9700a5 X:clojure.lang.LazySeq#1 Clq:clojure.lang.PersistentList$EmptyList#1
I assume that if I could see that x r p are at each call I might be able to see where the algorithm is going wrong.
Can anyone point me in the right direction?
EDIT: The neighV function from the SSCCE
(defn neighV [graph nodenum]
(let [ret-list (for [i (range (count graph)) :when (contains? (graph i) nodenum)] i)]
ret-list))
EDIT2: Noisesmith's answers had gotten me closer to the solution and made sense to me. I wrapped all of my concat in doall. After trying to call the function again I was getting "Cannot cast Long to Seq" errors, I figured that these stemmed from trying to concat loop-cnt onto lists in the function
fptests.core> (BK-Call (sanity1))
IllegalArgumentException Don't know how to create ISeq from: java.lang.Long clojure.lang.RT.seqFrom (RT.java:505)
fptests.core> (concat 1 '(2 3))
IllegalArgumentException Don't know how to create ISeq from: java.lang.Long clojure.lang.RT.seqFrom (RT.java:505)
So I then wrapped each loop-cnt in a '() to turn it into a list before it is concat
fptests.core> (concat '(1) '(2 3))
(1 2 3)
Which, after I made all of these changes, I ended back at my stack overflow.. Here is the new Bron-Kerbosch function with all of the edits. I guess I now have the same questions as I did before..
Though the new ones are, did I implement that changes that I should have correctly, does the usage of '() make sense to fix the issue that came up after implementing noisesmith's changes?
(defn Bron-Kerbosch1 [r p x graph cliques]
(cond (and (empty? p) (empty? x)) (doall (concat cliques r))
:else
(let [neigh (neighV graph (dec (count p)))]
(loop [loop-clq '(cliques)
loop-cnt (dec (count p))
loop-p '(p)
loop-x '(x)]
(cond (= -1 loop-cnt) loop-clq
:else
(recur (doall (concat loop-clq
(Bron-Kerbosch1 (doall (concat r '(loop-cnt)))
(doall (concat p neigh))
(filter (set x) neigh)
graph cliques)))
(dec loop-cnt)
(filter (set p) '(loop-cnt))
(doall (concat x '(loop-cnt)))))))))
EDIT3: After patiently waiting for my prn statements to stop (not sure why I put them in when I knew it was in a SO) I found that most if not all statements printed were along the lines of
"R:" (loop-cnt loop-cnt loop-cnt loop-cnt loop-cnt loop-cnt loop-cnt ...)
"P:" (range (count graph) 0 2 3) " X:" () " Clq:" ()
After inspecting this some I realized that I have not been recursively calling the function properly. I have been union'ing items to P instead of removing them. This causes P to continuously grow. This is most likely the cause of my stack overflow. Though there are still some issues. I still am creating a stackoverflow, yet again.
Once I fixed my issue of continuing to union to P my issue is that when I concat loop-cnt it is not, I guess to say, evaluated to a value but it stays as a variable name loop-cnt. I suspect that my stack overflow now lies with my bootstrap condition not being met because it cannot be met if loop-cnt is not evaluated to a number.
So I think my issue now lies with concat loop-cnt to a list as a number and not a variable.
concat is lazy. Recursive calls that build concat on top of concat, without realizing any of the prior layers of laziness, each add to the size of the stack of calls that will be needed to realize the lazy-seq.
Does this concatenation need to be lazy at all? If not, wrap the calls to concat in calls to doall. This will make the concatenation eager, which reduces the size of the call stack needed to realize the final result, and thus eliminating the StackOverflowError.
Also, the correct way to print a lazy-seq and see the contents is prn, you can use pr-str to get the form of the value that pr or prn would use as a string, if needed.
You are misusing quoted lists, I think.
For example, in (defn Bron-Kerbosch1 [ ... ] ... ), '(cliques) evaluates to a list containing the symbol cliques, not to a list containing the argument cliques as its one element. You want (list cliques) for that.
Similar cases abound.

Clojure returns the list of all pairs in seq that have key as their first element

I need to define a function called
(get-all-pairs key seq)
It returns the list of all pairs in seq that have key as their first element. If no pairs match, then the empty list is returned.
For example,if I def pets
(def pets
'((cat 1) (dog 1) (fish 1) (cat 2) (fish 2))
)
(get-all-pairs 'cat pets) returns ((cat 1) (cat 2)), and (get-all-pairs 'bird pets) returns '().
Here is my try:
(defn get-all-pairs [key seq]
(cond
(= key (first(first(seq)))) (cons (first seq)
(get-all-pairs key (rest seq)))
:else '()))
But it doesnot work. If I call it, it messages as follow:
#'proj2.proj2/pets
=> (get-all-pairs 'cat pets)
ClassCastException clojure.lang.PersistentList cannot be cast to clojure.lang.IFn proj2.proj2/get-all-pairs (proj2.clj:20)
I don't know where the problem is. How to fix it?
The immediate error is because you have too many parens in your definition: (first (first (seq))) should just be (first (first seq)). Once you fix that, your function should run to completion, but give you the wrong answer: think about whether () is really what you want in your else case.
Once you've worked out your by-hand recursive approach, try to figure out what is going on in this solution:
(defn get-all-pairs [k pairs]
(filter #(= k (first %)) pairs))
You are on the right track, but sort of over-thinking this one. For allows you to abstract away many of the things you are attempting to do manually. In this case, we can generate a seq and iteratively use an if expression with :when.
(defn get-all-pairs [animal L]
(for [k L
:when (= animal (first k))]
k))

What's expecting a function in this clojure code?

going through the exercises in the fp-oo book and i'm having trouble with an early exercise to add squares. Here's my code:
(defn square [n]
(* n n))
(defn add-squares [l]
(cond
(nil? l) 0
:else (+ (square (first (l))) (add-squares (rest (l))))))
This example:
(add-squares '(2 2 2 2))
should return
16
but fails with this exception:
ClassCastException clojure.lang.PersistentList cannot be cast to clojure.lang.IFn user/add-squares (NO_SOURCE_FILE:4)
which i guess means i'm trying to pass a function somewhere instead of a list which was expected. Can someone tell me which part of the code is the culprit?
Thanks,
James
This is wrong:
(first (l))
This means that you are calling l as a function.
You should use:
(first l)
And of course, the same thing for (rest (l))

Clojure fibonacci series

I keep getting a java.lang.IllegalArgumentException: Don't know how to create ISeq from: four_cloj.core$my_fib
(defn my-fib []
(let [a 0 b 1]
(lazy-seq (cons a (my-fib b (+ a b))))))
Anyone got any ideas?
Your current issue is that you are recursively calling my-fib with two arguments.
(my-fib b (+ a b))
But, the function argument list for my-fib contains no arguments
(defn my-fib []

Grouping a sequence of bools in clojure?

I would like to transform the following sequence:
(def boollist [true false false false true false true])
Into the following:
[[true] [false false false true] [false true]]
My code leads to a Stackoverflow:
(defn sep [boollst]
(loop [lst boollst
separated [[]]
[left right] (take 2 lst)]
(if (nil? left) separated)
(recur (next lst)
(if (false? left)
(conj (last separated) left)
(conj separated [left]))
(take 2 (next lst)))))
Is there an elegant way of transforming this?
There's probably a much more elegant way, but this is what I came up with:
(defn f
([xs] (f xs [] []))
([[x & xs :as all] acc a]
(if (seq all)
(if x
(recur xs [] (conj a (conj acc x)))
(recur xs (conj acc x) a))
a)))
It just traverses the sequence keeping track of the current vector of falses, and a big accumulator of everything so far.
A short, "clever" solution would be:
(defn sep [lst]
(let [x (partition-by identity lst)]
(filter last (map concat (cons [] x) x))))
The "stack overflow" issue is due to the philosophy of Clojure regarding recursion and is easily avoided if approached correctly. You should always implement these types of functions* in a lazy way: If you can't find a trick for solving the problem using library functions, as I did above, you should use "lazy-seq" for the general solution (like pmjordan did) as explained here: http://clojure.org/lazy
* Functions that eat up a list and return a list as the result. (If something other than a list is returned, the idiomatic solution is to use "recur" and an accumulator, as shown by dfan's example, which I would not consider idiomatic in this case.)
Here's a version that uses lazy evaluation and is maybe a little more readable:
(defn f [bools]
(when-not (empty? bools)
(let
[[l & r] bools
[lr rr] (split-with false? r)]
(lazy-seq (cons
(cons l lr)
(f rr))))))
It doesn't return vectors though, so if that's a requirement you need to manually pass the result of concat and of the function itself to vec, thus negating the advantage of using lazy evaluation.
The stack overflow error is because your recur is outside of the if. You evaluate the if form for side effects, then unconditionally recur. (feel free to edit for format, I'm not at a real keyboard).