I am having a hard time tracking down a stackoverflow error produced by my clojure implementation of the [Bron-Kerbosch algorithm](http://en.wikipedia.org/wiki/Bron%E2%80%93Kerbosch_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.