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 |

papa said...


final year project
mini projects for cse
final year projects for cse
final year projects for cse students
final year projects for cse domains
final year projects for cse in data mining
final year projects for cse with source code
final year project for ece
final year project in mechanical engineering
final year project for eee

invincible01 said...

Amazing Article,Really useful information to all So, I hope you will share more information to be check and share here.

inplant training for biotechnology in chennai
inplant training for ece students
inplant training mechanical engineering students
inplant training certificate format for civil engineering
inplant training report ppt
inplant training report samples
inplant training letter format
inplant training report for civil engineering pdf
inplant training report for electrical engineering