Fork me on GitHub
#core-logic
<
2016-04-06
>
josh.freckleton04:04:01

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)]))

josh.freckleton04:04:27

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

hiredman20:04:54

@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

josh.freckleton20:04:56

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

josh.freckleton22:04:11

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)))

josh.freckleton22:04:35

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...