سؤال

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