core-logic

2016-04-06T04:08:01.000060Z

How can I get all multiples of3 in range 50 using core.logic? This is all I've got:

(run* [q]
  (membero q (range 50))
  (conde
   [(== (mod q 3) 0)]))

2016-04-06T04:08:27.000061Z

I get the error clojure.core.logic.LVar cannot be cast to java.lang.Number

2016-04-06T20:10:54.000063Z

@josh.freckleton: the thing is, core.logic doesn't have its own notion of names, so when you see 'q' there what you are seeing is a clojure name 'q' which is bound to an lvar. mod of course is a clojure function which has no idea what to do with an lvar. the only meaningful operations on lvars are constraints (via unification or something else). for numeric stuff often what you want can be achieved using https://github.com/clojure/core.logic/wiki/Features#clpfd

2016-04-06T20:11:56.000065Z

@hiredman: thanks, I'm spending some time with "the reasoned schemer", and some of this is starting to become clear, thanks for your help!

2016-04-06T22:29:11.000066Z

can I ask another question? I'm trying to sum up all multiples of 3 and 5, and I've got everything working except I can't connect the sumo to the list of multiples of 3/5:

(defnc divxc [x y] ; is it divisible?
  (zero? (mod x y)))

(defn sumo [x out] ; sum up a list
  (conde
   ((emptyo x) (== out 0))
   (s# (fresh [f r rs]
         (firsto x f)
         (resto x r)
         (sumo r rs)
         (fd/+ f rs out)))))

(defn mult35o [x] ; list of things divisible by 3 or 5
  (fresh [a]
    (fd/in a (fd/interval 1 50))
    (fd/distinct [a])
    (conde
     ((divxc a 3))
     ((divxc a 5)))
    (== x a)))

(run* [x] ; test
  (mult35o x))
(run* [x] ; test
  (sumo [1 2 3 4] x))

(run* [x]
  (fresh [a b]
    (mult35o a)
    (firsto a b)
    (sumo b x)))

2016-04-06T22:30:35.000067Z

it seems that a is getting assigned something like ((3 5 ...)), so I try to "unpack" it with firsto, but in the end this doesn't work...

2016-04-06T22:30:38.000068Z

😞