This page is not created by, affiliated with, or supported by Slack Technologies, Inc.

## 2020-02-10

## Channels

- # announcements (2)
- # babashka (15)
- # beginners (174)
- # calva (16)
- # chlorine-clover (10)
- # clara (7)
- # clj-kondo (27)
- # cljdoc (10)
- # clojars (4)
- # clojure (50)
- # clojure-europe (10)
- # clojure-gamedev (3)
- # clojure-greece (1)
- # clojure-italy (4)
- # clojure-losangeles (1)
- # clojure-nl (16)
- # clojure-sg (1)
- # clojure-spec (7)
- # clojure-uk (41)
- # clojurescript (22)
- # data-science (15)
- # datascript (38)
- # datomic (2)
- # duct (15)
- # emacs (2)
- # fulcro (110)
- # funcool (9)
- # graphql (10)
- # jackdaw (8)
- # jobs (23)
- # joker (1)
- # leiningen (15)
- # luminus (1)
- # malli (20)
- # off-topic (26)
- # pathom (5)
- # pedestal (1)
- # reitit (19)
- # shadow-cljs (78)
- # spacemacs (2)
- # sql (52)
- # tools-deps (99)
- # vim (13)

Clojure/core.logic looks fun, and I'd love to be able to use it just to say I'd done some logic programming. But I'm not clear on what kinds of problems I would really solve with it. I've read that logic programming is good for querying problems, are there any other categories you would consider well suited? I know being a hammer looking for nails is silly, but what can you do 🤷

Based on what I know, logic programming is ONLY good for querying problems. Other problems can be solved if and only if you can represent the problem as a querying problem.

I guess the question is then, are there loads of problems that can/should be phrased as querying problems and I just don't how how to think that way, or is this tool just pretty niche

Honest answer is I don't know. I am of the opinion that logic programming is more academic although there are definitely a handful of practical uses. That is why we don't see it in mainstream programming so much.

Last year I gave a coding dojo on logic programming (using Clojure). I did it because I don’t know too much about logic programming myself, and I’m curious about it. How better to learn than by teaching others, right? 😉 Anyway, a few months later I was doing a couple of Advent of Code puzzles, and then I realized that I could solve one of the “part 2”s super simply by using logic programming. So bottom line: maybe it has to become part of your toolbox before you can identify the situations where it makes sense to use. (For the curious, it was day 2 part 2, and my code is here: https://github.com/svdo/advent-of-code-2019/blob/master/src/aoc/day2.clj).

Another thing to add to your toolbox is https://github.com/clojure/core.match. It can lead to very succinct yet readable code *when the situation is right*.
Here's FizzBuzz,

```
(require '[clojure.core.match :refer [match]])
(doseq [n (range 1 101)]
(println
(match [(mod n 3) (mod n 5)]
[0 0] "FizzBuzz"
[0 _] "Fizz"
[_ 0] "Buzz"
:else n)))
```

Any queries on data I've needed to do can be written as a simple filter statement so far (hobby project) so I don't think it would be upholding simplicity to shoe horn it in there 😁

(re-reading your question, I'm getting the impression that you were asking more broadly than for a specific task)

That's an interesting puzzle! I was thinking broadly, somehow being able to use it for good in my existing hobby projects would be best, but I'm not apposed to just solving a weird puzzle as well

Examples from its Wiki: https://github.com/clojure/core.logic/wiki/Examples

Yeah, it is constraint programming (is this the right word?) where you define a whole bunch of constraints and ask it to search the solution space for one that satisfies all the constraints.

one of my favorite projects here: https://github.com/hiredman/Archimedes

That's some cool stuff! The Sudoku program is so short.

A Sudoku solver is short in Clojure either way, logic programming or not. The main benefit of core.logic is you only have to teach it the rules. You don't have to teach it HOW to reach the solution.

A related question. Suppose you have a bunch of products of different levels of complexity and a bunch of recipes that tell you how to get a particular product from a set of other products, including how much time and resources it would take to make a single specific product and what byproducts will be created. Some recipes can output the same very thing they require, but perhaps a different quantity. The task: given a particular product, find all combinations of recipes that can create it. Ideally, also allow to optimize for some particular product consumption or for total time. How would you approach such problem?

I can imagine how logic programming would be great at determining the recipe dependencies, because that’s very query-like (and recursive). I’m unsure tho how you’d express the optimization part of the problem...it’s an aggregate min/max.

My CS Theory is a little rusty but I remember this as Constraint Optimization. Lots of candidate solutions can be generated, then there are constraints that reduce the size of the solution space and order remaining candidates. Implementations of these algorithms are generally called "solvers": https://developers.google.com/optimization/cp In my youth I spent some time studying genetic programming, where the "recipes" are actual instruction sequences and the costs are e.g. execution time, memory use, etc. There is a professor who has a Clojure implementation of GP: https://github.com/lspector/Clojush