-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 figured it out, i the () were a bit messed up. the working code:
(def open 0) (def p1 1) (def p2 2) (def emptyfield [open open open open open open open open open]) (defn indexOf [x y] (+ x (* y 3))) (defn withmove [x,y,player,field] (assoc field (indexOf x y) player)) (defn winner [field] (letfn [(slashOwnedBy [player] (= [player] (distinct [(nth field 0) (nth field 4) (nth field 8)]))) (backslashOwnedBy [player] (= [player] (distinct [(nth field 2) (nth field 4) (nth field 6)]))) (rowOwnedBy [row player] (let [beginIndex (indexOf 0 row) currow (subvec field beginIndex (+ 3 beginIndex))] (= [player] (distinct currow)))) (colOwnedBy [col player] (let [beginIndex (indexOf col 0) curcol (take-nth 3 (drop beginIndex field))] (= [player] (distinct curcol)))) (winPred [player] (loop [cnt 0] (if (= cnt 3) (or (slashOwnedBy player) (backslashOwnedBy player)) (or (rowOwnedBy cnt player) (colOwnedBy cnt player) (recur (inc cnt))))))] (let [winnerIfExists (filter winPred [p1 p2])] (if (empty? winnerIfExists) open (first winnerIfExists))))) (let [moves [[2 0 p1] [1 1 p1] [0 2 p1]]] (defn fold [field nextmove] (withmove (nth nextmove 0) (nth nextmove 1) (nth nextmove 2) field)) (let [endstate (reduce fold emptyfield moves)] (println (winner endstate)))) a few questions: is there a better way than (= player (distinct currow)) ? i'd like to write something like: (every? (= parameter player) currow do i have to define the function via letfn before, or is there a way to do it nested in the code? and more importantly: is there an ide that can point out syntax errors? intellij idea can detect some parentheses/braces problems, but i managed to trick it by adding too many ( and ). i got weird exceptions and had to check everything manually. Am 04.09.2011 19:57, schrieb Sergey Didenko: > Dennis, may I suggest you to read this great article on Clojure: > http://java.ociweb.com/mark/clojure/article.html > > > > -- You received this message because you are subscribed to the > Google Groups "Clojure" group. To post to this group, send email to > clojure@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+unsubscr...@googlegroups.com For more options, visit this > group at http://groups.google.com/group/clojure?hl=en - -- -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.14 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iQIcBAEBAgAGBQJOZiGMAAoJENRtux+h35aGuSwP+gPkuR9tAyHUKUhqCrkFgtiJ cuuD47EHMyws/IMwqra1RTBaCAaZbJLx9m4sFn9/KLhbGni7fzTt+zAJy1yxyW21 RaqZJC43VgG6xmDQYVt54aY9zzHz3MkI3eXZBY6Wsd0yquu2NZjnleEvoUNzwsnI nLi0nsyLkLMX6b/QdDj48XaatfeG48i5M+H9MlF7EpesoIntUEHhXRVhK1BnOeCk ctd4bo0brdszBes7UhZl9abisaiW1vf+rWAAD8HPqcFg4tWUS//SRRmFyEUJtusc GXwiPI5Meu0fKHKJv5EhQl87BD3lG+vLUn1XspYYmrZKn5G0ZxL7v/L5ol1g7Zs4 doSVsWCkR3zMwyaQgrvj/IuvIMxhWhgXGYwdEBHstPJephlwCRC5sOQCLvjwJB6H LEV0nx3m2GiWGH+jOqJ2M+j/OwsYNfPxEPRICLpUjm3lR+Whb1l+OL0fMwREgYkS ySeto5Q2pI+pkwZc1PsM2YMUEBjBToZroIlMZw6N6IUuQPFNJBcHPakRNDUs93Du nmg8kCGy+thBedbky7KxDW1bmBNFYxEWR2iASXkkIjhUdyrn3hVOxz1D1a6rM3Cb S2CdXZG5HeiCsqghyQ/m36bqlLf8CTsFz60lb4Q0Wncpn/EDTSyYq76RRPfGoMBu Nec7CzUkF8sat1lrZKxy =v1mG -----END PGP SIGNATURE----- -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en