gpt4 book ai didi

clojure - 如何在 core.logic 中模拟 'outer join'?

转载 作者:行者123 更新时间:2023-12-03 14:54:36 24 4
gpt4 key购买 nike

我刚刚开始使用 core.logic,为了解决这个问题,我正在尝试实现一些与我目前正在专业处理的问题类似的简单问题。然而,问题的一部分让我难住了......

作为我的示例的简化,如果我有一个商品目录,其中一些仅在某些国家/地区可用,而有些在特定国家/地区不可用。我希望能够指定项目列表和异常(exception)情况,例如:

(defrel items Name Color)
(defrel restricted-to Country Name)
(defrel not-allowed-in Country Name)

(facts items [['Purse 'Blue]
['Car 'Red]
['Banana 'Yellow]])

(facts restricted-to [['US 'Car]])

(facts not-allowed-in [['UK 'Banana]
['France 'Purse]])

如果可能,我宁愿不为所有国家/地区指定允许进入,因为具有限制的项目集相对较小,并且我希望能够进行一次更改以允许/排除给定的项目国家。

我如何编写一个规则来给出一个国家的项目/颜色列表,并有以下限制:
  • 该项目必须在项目列表中
  • 国家/项​​目不得在“不允许进入”列表中
  • 任何一个:
  • 该项目的限制列表中没有国家/地区
  • 国家/地区/项目对在限制列表中

  • 有没有办法做到这一点?我是否以完全错误的方式思考问题?

    最佳答案

    通常,当您开始否定逻辑编程中的目标时,您需要达到非关系操作(在 Prolog 中切入,在 core.logic 中使用 conda)。

    此解决方案应仅使用基本参数调用。

    (defn get-items-colors-for-country [country]
    (run* [q]
    (fresh [item-name item-color not-country]
    (== q [item-name item-color])
    (items item-name item-color)
    (!= country not-country)

    (conda
    [(restricted-to country item-name)
    (conda
    [(not-allowed-in country item-name)
    fail]
    [succeed])]
    [(restricted-to not-country item-name)
    fail]
    ;; No entry in restricted-to for item-name
    [(not-allowed-in country item-name)
    fail]
    [succeed]))))

    (get-items-colors-for-country 'US)
    ;=> ([Purse Blue] [Banana Yellow] [Car Red])

    (get-items-colors-for-country 'UK)
    ;=> ([Purse Blue])

    (get-items-colors-for-country 'France)
    ;=> ([Banana Yellow])

    (get-items-colors-for-country 'Australia)
    ;=> ([Purse Blue] [Banana Yellow])

    Full solution

    关于clojure - 如何在 core.logic 中模拟 'outer join'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8705001/

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