I need to write some core.logic code that checks whether two out of three goals succeed.

I know I can write it like this:

(run* [a b c]
      (conde [goal1 goal2]
             [goal2 goal3]
             [goal3 goal1]))

...but this is cumbersome, and I actually need to generalize my code for the "N out of M" case, and this will be difficult to generalize. Can someone point me to the right approach to use for this type of problem? Is there a function available that simplifies this?

Thanks!

有帮助吗?

解决方案

Probably a macro can help:

(defmacro n-of-m-goals [n all-goals]
   `(conde ~@(combinations all-goals n))

Then your example would become:

(run* [a b c]
      (n-of-m-goals 2 [goal1 goal2 goal3]))

combinations is from math.combinatorics

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top