This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-08-14
Channels
- # announcements (31)
- # babashka (9)
- # beginners (4)
- # calva (67)
- # cider (6)
- # clj-yaml (10)
- # clojure (105)
- # clojure-austin (8)
- # clojure-bay-area (1)
- # clojure-europe (12)
- # clojure-germany (3)
- # clojure-nl (1)
- # clojure-norway (7)
- # clojure-uk (2)
- # clojurescript (5)
- # core-logic (4)
- # data-science (29)
- # datomic (6)
- # dev-tooling (5)
- # emacs (3)
- # hyperfiddle (22)
- # introduce-yourself (4)
- # lsp (8)
- # malli (10)
- # off-topic (8)
- # pathom (74)
- # polylith (39)
- # practicalli (1)
- # reitit (3)
- # shadow-cljs (2)
- # spacemacs (3)
- # squint (4)
- # tools-deps (4)
Trying to wrap my head around core.logic. How can I mix functional and logic programming?
(defn squared [x]
(* x x))
(run* [x]
(membero x [1 2 3])
(fd/< (squared x) 9)
(fd/> (squared x) 1))
This does not work (`clojure.core.logic.LVar cannot be cast to java.lang.Number`). Do I have to rewrite my function square
into a goal? How would I do that?You would need to turn it into a relation/goal. When turning a function into a relation, the most important step for reasoning about things (at least in my book) is to make the arity -> arity+1. Basically, you are turning the output of the function into one of the inputs of a relation. So for this problem
(defn squared [n]
(* n n))
Becomes
(defn squaredo [n s]
(fd/* n n s))
Now to use this in the run* statement, there needs to be a fresh variable.
(l/run* [x]
(l/membero x [1 2 3])
(l/fresh [sq]
(squaredo x sq)
(fd/< sq 9)
(fd/> sq 1)))
So in this case, it makes sq the square of x and sq is just used wherever (squared x) was used in the original example.❤️ 1
Thank you @U049Q8NTLG3, this helped.
Is it idiomatic to use featurec
to "destructure" values from deeply nested data structures to "store" them in lvars so they can be used in other goals?