[Cs254f11] Fwd: Help me fix this loop

Lee Spector lspector at hampshire.edu
Tue Oct 11 21:02:22 EDT 2011


Yep -- almost.

Eliminating that last call gets rid of the "not in tail position" error, and then it's valid code that will even eventually terminate but not doing what was wanted because of the other issues. It will terminate as soon as 0 is chosen for y, which will happen eventually by random chance. When y is zero it will take the first branch because c is always 1 and therefore always less than 3, and because the mistake in the "get" call (see previous mail) means that the default value of y will always be returned from "get", and when that happens to be zero we'll do the first branch of the "if", which is the call to "place-peice", and then the loop ends.

Note that this *will* set *one* (and only one) of the positions to 1 or 2. But you have to look carefully to see that in the result.

 -Lee


On Oct 11, 2011, at 8:02 PM, Jack Laxson wrote:

> 
> 
> ---------- Forwarded message ----------
> From: Jack L. <jackjrabbit at gmail.com>
> Date: Tue, Oct 11, 2011 at 7:58 PM
> Subject: Re: [Cs254f11] Help me fix this loop
> To: Maxwell William Fair Levit <mwl10 at hampshire.edu>
> 
> 
> Running the second loop without the last bit works, e.g. ...
> 
> (loop [c 1 x (rand-int 19) y (rand-int 19)]
>  (if (and (< c 3) (zero? (get @board x y)))
>    (place-peice x y (if (even? x) 1 2))
>    (recur c (rand-int 19) (rand-int 19)))
>  )
> 
> Lists all the positions but they're all 0. :)
> 
> Jack
> 
> On Tue, Oct 11, 2011 at 7:13 PM, Maxwell William Fair Levit <mwl10 at hampshire.edu> wrote:
> 
> 
> Hey all, I'm writing code that takes turns placing black and white (1 and 2)
> pieces on a Go board. I'm running into trouble with the loop that is supposed
> to actually place a bunch of pieces.
> 
> (def board
>  (atom
>    (zipmap
>      (reduce concat
>              (for [y (range 19)]
>                (map vector (repeat 19 y )
>                     (for [x (range 19)] x))))
>      (vec (repeat 361 0)))))
> 
> ;; defines a 19x19 board with all 361 entires set to 0.
> 
> 
> (defn place-peice
>  [x y z]
>  (swap! board update-in [[x y]] (fn [j] z) ))
> 
> ;;Sets the value of the space with coordinates [x y] to z.
> 
> (loop [c 1 x (rand-int 19) y (rand-int 19)]
>  (if (and (< c 3) (zero? (get @board x y)))
>    (do (place-peice x y (if (even? x) 1 2))
>        (recur (inc c) (rand-int 19) (rand-int 19)))
>    (recur c (rand-int 19) (rand-int 19))))
> 
> This just crashes clooj, presumably because its going infinitely, since I set
> the number of iterations low enough that it shouldn't just be an efficiency
> issue.
> 
> The other setup I had which gave me a "Must recur from tail position" error. was
> this.
> 
> 
> 
> (loop [c 1 x (rand-int 19) y (rand-int 19)]
>  (if (and (< c 3) (zero? (get @board x y)))
>    (place-peice x y (if (even? x) 1 2))
>    (recur c (rand-int 19) (rand-int 19)))
>  (recur (inc c) (rand-int 19) (rand-int 19)))
> 
> Can someone tell me why these don't work and/or how to fix them?
> 
> 
> Thanks,
> 
> -Max
> _______________________________________________
> Cs254f11 mailing list
> Cs254f11 at lists.hampshire.edu
> https://lists.hampshire.edu/mailman/listinfo/cs254f11
> 
> 
> _______________________________________________
> Cs254f11 mailing list
> Cs254f11 at lists.hampshire.edu
> https://lists.hampshire.edu/mailman/listinfo/cs254f11

--
Lee Spector, Professor of Computer Science
Cognitive Science, Hampshire College
893 West Street, Amherst, MA 01002-3359
lspector at hampshire.edu, http://hampshire.edu/lspector/
Phone: 413-559-5352, Fax: 413-559-5438



More information about the Cs254f11 mailing list