gpt4 book ai didi

clojure - 如何解构 core.logic 中的映射?

转载 作者:行者123 更新时间:2023-12-02 16:54:49 24 4
gpt4 key购买 nike

我相信我在解构 core.logic 中的 map 时遇到问题。我有以下代码:

... 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)))

它返回空列表(),表明没有找到匹配项。我相信这是代码的 (== {:rank a} v) 部分的问题。我试图简单地返回 q,其中 q 是按 :rank 降序排列的 map 向量。

最佳答案

现在可以使用最新的 core.logic 版本 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)))

关于clojure - 如何解构 core.logic 中的映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15821718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com