Skip to content

Instantly share code, notes, and snippets.

@swannodette
Last active December 11, 2015 22:59
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save swannodette/4673834 to your computer and use it in GitHub Desktop.
Save swannodette/4673834 to your computer and use it in GitHub Desktop.
(use '[clojure.core.logic])
(require '[clojure.core.logic.fd :as fd])
(defn simple []
(run* [x y]
(fd/in x y (fd/interval 0 9))
(fd/eq
(= (+ x y) 9)
(= (+ (* 4 x) (* 2 y)) 24))))
;; (simple) => [3 6]
:- use_module(library(clpfd)).
simple(Q) :-
Q = [X, Y],
4*X + 2*Y #= 24,
X + Y #= 9,
[X,Y] ins 0..9.
/* simple(Q). => Q = [3, 6] */
@bostonaholic
Copy link

what libraries must you first import for running the prolog version?

@jcolebrand
Copy link

Aren't lines 6 (clj) and 3 inverted math operations? 4 * y vs 2 * y?

@swannodette
Copy link
Author

@bostonaholic updated to include the necessary imports in both examples. @jcolebrand good catch, fixed.

@mrb
Copy link

mrb commented Jan 31, 2013

@swannodette One tiny thing -

(require '[clojure.core.logic.fd :as fd])

Tripped me up for a second.

@swannodette
Copy link
Author

@mrb Oops thanks for spotting that, fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment