This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn play [x o] | |
(letfn [(player [mark] (if (= :x mark) x o))] | |
(loop [mark :x board empty-board history []] | |
(show-board *view* board) | |
(let [opponent (opponent mark)] | |
(cond | |
(win-for? opponent board) {:winner opponent :board board :history history} | |
(draw? board) {:winner nil :board board :history history} | |
:else (let [m (move (player mark) board)] | |
(if (nil? (board m)) | |
(do (show-message *view* (format "%s takes %s\n" mark m)) | |
(recur opponent (assoc board m mark) (conj history m))) | |
(recur mark board history)))))))) |
(+ 1 2 3 4 5)Ruby
1 + 2 + 3 + 4 + 5In this example, I have to say Clojure wins on account of simplicity. It is easy to read and write this statement. I don't know if you can get any easier than this.
Enough background, here is how I would have started to learn Clojure:
- Blackstag - It is a really simple guide to get you started. You will not learn everything you need, but it's a good place to start. (I skipped installation because it's already installed on my computer so I'm not sure if it's still relevant.)
- Clojure API - I read most of the methods in here. It will help you become familiar with what Clojure has to offer.
- Roman Numerals Kata - Write your own roman numeral kata. I looked at others to see how they do it. Look up keywords that you don't understand.
- Read other people code. I look at several Clojure implementations of TicTacToe because mainly I have to write it. But it really helps because I have an idea of how the game is suppose to work. That is not to say I read and understand everything I read. Anyway, here's a list of Github you want to checkout:
- Rylan Tic Tac Toe
- Micah
- Colin
- Wai Lee
- Clojure Cookbook - I didn't look at it, but it sounds promising.
- Clojure Docs - This is my best friend once I got an idea of what function I want to call.
Note: I recommend using Emacs with rainbow parenthesis when you write Clojure code. If you are writing in Clojure, it is worth the time and effort to learn how to use Emacs.
I got most of my Clojure Tic Tac Toe code done by reading Blackstag, Clojure API, and others code. I am much more comfortable with the language now.
0 comments:
Post a Comment