[Cs254f11] Recursion without loop

Maxwell William Fair Levit mwl10 at hampshire.edu
Wed Nov 2 18:51:43 EDT 2011


Hey all, I'm rewriting the function that will determine the structure of a group
of stones and I'm a bit stuped.

Here is the function that finds the pieces next to a given piece that share a
color (rewritten very prettily by Lee)

(defn friends
  [x y]
  (let [c (get @board [x y])]
    (filter #(= (get @board %) c)
            [[(+ x 1) y]
             [(- x 1) y]
             [x (- y 1)]
             [x (+ y 1)]])))


What I want to do is take that function and call it on a given space, then call
it again on all the spaces it returns that haven't already been counted, and so
on until all pieces in the group have been counted.

What I have looks like this:


(defn lib-test
  [x y checked]
  (if (not (some #(= [x y] %) checked))
    (apply lib-test (friends x y) (conj checked [x y]))
         checked))

What I can't do is figure out how to apply (or map) lib-test to a bunch of
vectors, while having the extra argument 'checked' tacked on the end, but not
coming from the vectors.

Does this make sense? Can someone tell me how to make this work?

Thanks,

-Max


More information about the Cs254f11 mailing list