Wednesday, July 03, 2013

Getting my feet wet with Clojure's core.logic

Yesterday I read a nice article about implementing Event Sourcing in Clojure. The domain that the author had chosen to illustrate the concept was the rock-paper-scissors game. The compare-moves multimethod seemed a bit ugly to me, so I thought it would be a great opportunity to try logic programming in Clojure. Here is what I did:

1. Create a new Clojure project using Leiningen:
2. Add clojure.core.logic to project dependencies list:
3. Tell Leiningen to install the dependencies:
4. Write the rules of the rock-paper-scissors game in src/rock_paper_scissors/core.clj:
5. Start a REPL and play a little:
I hope you enjoy the declarative style! I surely do and will certainly continue my exploration of the awesome world of logic programming.


Will Byrd said...

Time to run the program backwards, and generate games of RPS. :)

Unknown said...

This was fun.

When trying to follow along, I found that clojure.core.logic has deprecated "defrel" and "fact" in favor of new syntax. I got it working with the following, using pldb/db-rel and pldb/with-db:

(ns rock-paper-scissors.core
(:refer-clojure :exclude [==])
(:require [clojure.core.logic.pldb :as pldb]
[clojure.core.logic :refer :all]))

(pldb/db-rel beats hand1 hand2)
(def facts
[beats :rock :scissor]
[beats :paper :rock]
[beats :scissor :paper]))

(defn play [hand1 hand2]
(pldb/with-db facts
(run* [result]
((beats hand1 hand2) (== result :victory))
((beats hand2 hand1) (== result :loss))
((== hand1 hand2) (== result :tie)))))))

Anonymous said...

viagra asli
obat viagra
viagra original

Anonymous said...

Hey, Wow all the posts are very informative for the people who visit this site. Good work! We also have a Website. Please feel free to visit our site. Thank you for sharing.Well written article Thank You Sharing with Us project management courses in chennai | pmp training class in chennai | pmp training fee | project management training certification | project management training in chennai | project management certification online |