3

this is my first question on Stack Overflow.

I’m new to logic programming and are trying to evaluate if it can be used to solve some matching problems I’m working on.

Problem:

Lets say we have a set A that looks like this.

A = {1, 2, 3, 4}

And then we have some other sets that looks like this.

B = {1, 2}
C = {3, 5, “banana"} 
D = {2, 3, 4}

The problem I’m trying to solve is,

"Find me the set that share the most members with set A, compared to the other sets we know about."

The answer in this case should be set D, because it shares three members with set A. Compared to the other sets that only share two and one member with A.

Question 1:

Can Logic Programming solve this types of problems?

Question 2:

If it can, how would you do it in for example Clojure’s core.logic?

Community
  • 1
  • 1
qeshi
  • 85
  • 7
  • 1
    I don't see why logic programming would be needed here. A simple set intersection is all you need. Check out http://clojure.github.io/clojure/clojure.set-api.html – ClojureMostly May 31 '15 at 16:12

1 Answers1

-1

Qeshi

The following shows getting the best fit result using clojure.set:

(ns
  sample.sandbox
  (:require [clojure.set :as set])
  )

(def A #{ 1, 2, 3, 4})
(def B #{1, 2})
(def C #{3, 5, "banana"})
(def D #{2, 3, 4})

(defn best-fit-set
  [control & sets]
  (apply max-key count (map #(set/intersection control %) sets )))

(best-fit-set A B C D) => #{4 3 2}
Frank C.
  • 7,758
  • 4
  • 35
  • 45
  • This doesn't answer what is asked. – Leon Grapenthin Jun 01 '15 at 16:53
  • The problem I have at hand is actually more complicated. but I thought I had to simplify it in order to make it fit here on Stackoverflow. Maybe this is the best solution for this problem? Because I feel that as a beginner you usually learn a few techniques, that you then try to apply to all problems. Like you learn how to use a hammer, and then you try to solve all your problems with a hammer, even though there are other ways that are better and maybe even simpler. I think I’ll post this question again but with more complexity but try to keep it general enough. – qeshi Sep 27 '15 at 08:38