I believe I am having trouble destructuring a map in core.logic. I have the following code:
... used clojure.core.logic
... required clojure.core.logic.arithmetic as logic.arithmetic.
(def hand ({:rank 9, :suit :hearts}
{:rank 13, :suit :clubs}
{:rank 6, :suit :spades}
{:rank 8, :suit :hearts}
{:rank 12, :suit :clubs}))
(run* [q]
(fresh [v w x y z] ;;cards
(== q [v w x y z])
(membero v hand)
(membero w hand)
(membero x hand)
(membero y hand)
(membero z hand)
(fresh [a b c d e] ;;ranks
(== {:rank a} v)
(== {:rank b} w)
(== {:rank c} x)
(== {:rank d} y)
(== {:rank e} z)
(logic.arithmetic/>= a b)
(logic.arithmetic/>= b c)
(logic.arithmetic/>= c d)
(logic.arithmetic/>= d e))
(distincto q)))
It returns the empty list (), indicating that it found no matches. I believe it is a problem in the (== {:rank a} v) portion of the code. I am attempting to simply return q, where q is a vector of the maps in :rank descending order.
A much more concise solution can now be written using the latest core.logic release 0.8.3:
(ns cards
(:refer-clojure :exclude [==])
(:use [clojure.core.logic])
(:require [clojure.core.logic.fd :as fd]))
(def hand
[{:rank 9, :suit :hearts}
{:rank 13, :suit :clubs}
{:rank 6, :suit :spades}
{:rank 8, :suit :hearts}
{:rank 12, :suit :clubs}])
(defn ranko [card rank]
(featurec card {:rank rank}))
(run* [v w x y z :as q]
(permuteo hand q)
(fresh [a b c d e]
(ranko v a) (ranko w b) (ranko x c)
(fd/>= a b) (fd/>= b c)
(ranko y d) (ranko z e)
(fd/>= c d) (fd/>= d e)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With