-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
i started with a tic tac toe implementation, but i'm stuck:
(def open 0)
(def p1 1)
(def p2 2)
(def emptyfield [open open open open open open open open open])
(defn updated [seq index replacement]
(concat
(take index seq)
[replacement]
(drop (inc index) seq)))
(defn indexOf [x y] (+ x (* y 3)))
(defn withmove [x,y,player,field]
(updated field (indexOf x y) player))
(defn winner [field]
(defn winPred [player]
(defn rowwin [row]
(let [beginIndex (indexOf 0 row)
currow (subvec beginIndex (+ 3 beginIndex) field)]
(defn ownedByPlayer [value])
every? ownedByPlayer currow))
(defn colwin [col]
(let [beginIndex (indexOf col 0)
curcol (take-nth 3 (drop beginIndex field))]
(defn ownedByPlayer [value])
every? ownedByPlayer curcol))
(loop [cnt 0]
(if (= cnt 3) false (or (rowwin cnt) (colwin cnt) (recur (inc
cnt))))))
(let [winnerIfExists (filter winPred [p1 p2])]
(if (empty? winnerIfExists) open (first winnerIfExists))))
(let [moves [[0 0 p1] [1 0 p1] [2 0 p1]]]
(doall
(for [move moves]
(let [x (nth move 0)
y (nth move 1)
player (nth move 2)]
(print "player ")
(print player)
(print " makes move at ")
(print x)
(print "/")
(println y)
))
)
)
two questions:
* in the last loop where i am just printing out what i want to do, i
need something like "foldLeft" (from scala). how do i fold in clojure?
* is there no predefined "updated" function?
Am 03.09.2011 23:38, schrieb Luc Prefontaine:
> On Sat, 3 Sep 2011 13:43:42 -0700 (PDT) HamsterofDeath
> <[email protected]> wrote:
>
>> this might seem like a stupid question, but for me, not knowing
>> the type of something is like being stuck in a dead end for
>> anything non trivial.
>
> It's not stupid, it's normal :)
>
> In functional programming, most of the time you would like to write
> functions that do not need to know their arguments too intimately.
> You would like to work on collections, maps, ...
>
> Of course at some point you will need to write functions that will
> look closer to their arguments.
>
> You can pass functions to generic ones and isolate that type
> knowledge within them. No need to spread this everywhere.
>
>> i've made a few little experiments with clojure (not much, just
>> testing some features) and i see how powerful clojure can be -
>> for small to medium sized problems with simple input and output -
>> a*- pathfinding, for example.
>>
>> but how would i port a complex object graph (let's say 15
>> different classes with 3-7 fields each - person, contacts,
>> orders, shipping details) to clojure? how would i handle it?
>
> defrecord might help you a bit here. It may feel a bit "like
> home". defrecord fields can be referenced as map entries
> (:field-name ...).
>
> You can also define protocols that associated with defrecord and
> may ease your pain by implementing familiar functions to navigate
> in the hierarchy.
>
> Not sure if using a library written by someone else to handle these
> things is the proper thing to do right now. I feel you need to
> break your teeth a bit :) (It took me three months to get used to
> immutability :))
>
>> the main problems i see are: * do i have to actually remember the
>> complete structure and field names and the exact spelling? using
>> statically types languages like java or scala, the ide
>> autocomplete features really help here.
>
> If you use obvious names that match the problem domain this should
> be easy to overcome. Protocols could help you here by hiding some
> complex navigation but please refrain implementing "getters" for
> all individual fields :))
>
>> * what about renaming a field or method?
>
> Yep of course you will not have this nice refactoring feature where
> you type in place the new name and get the IDE to fix this
> everywhere for you. But on the other hand you should have at least
> 10 times less code compared to java and less side effects to
> debug. It should not be too hard to do this using a standard text
> search. I use Eclipse and the straight "file" search. I would never
> exchange Clojure for Java and the automated "Refactoring"
> commands.
>
> If you encapsulate frequently exposed fields in functions you
> should be able to reduce the footprint of the code where these
> things are exposed. Hence the name changes would be easy to
> implement. You would confine these functions in a specific name
> space which decrease the like hood of missing a change.
>
>> * if a function needs an instance of the root class of the
>> complex graph above as a parameter - how do i know this at all?
>> am i lost without good documentation of this function? in java, i
>> just know what a method needs because it has a signature.
>>
> Use the doc string when defining a fn:
>
> (defn blbl "Returns the meaningful "blblblbl..." string. It expects
> a single parameter, the length of the returned string" [length]
> ...)
>
> You can describe the expected inputs and the result, ... Do the
> same thing with your name space definitions, protocols. ...
>
> It's easy, fits with your programming flow and is non-obtrusive.
>
>
- --
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQIcBAEBAgAGBQJOY6WDAAoJENRtux+h35aGPpQP/1uNS5vy3cQ3PGb/kcJLw86b
TWgYaNFigA6ga0mK+C/bsiH9q0Akyi7vtLTuO58XklWjm68DhPQ0NiCRvPdOMKI+
m6/X6h9JVCvzyibkz11ftInSl7/b5Yf96LuX2UqdYDLtNYzadLBuaZxzPnppXbkI
mNhge+9hl8/qFLxBoqtr3Ov/1h6LNV9jv9awWAvoy3O5VgzS3NB3h6zxWkpUdU+u
Xc6xJQc58ZQDQA3pOq8zBhor64RibFDDV3KZ8OwJx7gSjGR7dC7Y0jYuJjsxC8f9
GZwTij8YkPOPjYVkXNn80QQhrIL+zJZUl3nSIWlx+4R2QOJmqMaADtId6JFVIoP9
8LL5yxix+EH4eES6D8csMIAytMondbAW3oGnlpA5n8SdbIaD0uSZcJBsv9ZPyyGB
YAdkI7vh08mxZnkMLSmziOHnigLMlXcZN2yYFtuMyqC4Elj3AqbKzdPa10wSDaF2
EXW5QcDj/Ud6DgRU1bo3sVERdGmUrKnXKuwhKX+1rskl0yLiXxGMTGOagmbcZ900
7lFsODDN8enXLE/Iv8M5VmFQTpr22U7OyNO2QVTGamlzg6SRvRBLATdr//5H5Zs5
CukIdhTXmS96weut9t8yxfsF859ZyjgRB94j3PYyPnJ91A8fFkjChumlhV4UIwV9
8YMnIdFmBwUYoiySsQEk
=vfQt
-----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 [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en