3 ms·
My biggest problem with logic programming is that my imperative brain can't make the jump to thinking in those terms. Maybe if someone could post a bunch of exa
by FractalLP 9y ago
My biggest problem with logic programming is that my imperative brain can't make the jump to thinking in those terms. Maybe if someone could post a bunch of examples comparing idiomatic python to prolog I could start to think in those terms. There is a 99 problems in Prolog, but those aren't exactly what I think is needed. I think that should also be done for functional programming, but I find FP far easier to grok.
- triska 9y agoIn a sense, if you already understand functional programming, then the leap to logic programming is comparatively straight-forward: Say you have a function f with N arguments that yields a value y. That is, you have y = f(x_1,...,x_N). Then you can regard such a function as a relation with (N+1) arguments, relating the N arguments to the intended result y. So, in other words, the function can be modeled as a predicate P(x_1, ..., x_N, y) that holds iff y = f(x_1, ..., x_N). The key differences between functions and predicates are: 1. In contrast to a function, a predicate can relate the same arguments to multiple (different) results. 2. You can query a predicate in all directions, also if none of the arguments and not even the result is known, and it will still often yield useful answers. In fact, it no longer even makes sense to regard one particular argument as the "result". A predicate simply holds for certain combinations of arguments, and does not hold for others. Hence, you can consider predicates as a natural generalization of functions, and if you are already familiar with functional programming, logic programming will likely be comparatively easy to grasp. A large number of the 99 problems you mention are very imperatively worded, and if you solve them only in the way they are phrased then you will benefit only little from the true potential of Prolog. For example, the task descriptions ask you to "find", "reverse", "flatten" and "eliminate", but rarely to actually express the relation between things so that you can use the predicate in all directions and thus truly benefit from logic programming. Ideally, the same Prolog predicate can be used to generate all solutions, validate any given solution, to compute a specific solution etc.
- YeGoblynQueenne 9y agoIf you want to see a discussion of Prolog alongside a functional and an imperative language, there's George Luger's textbook, "AI Algorithms, Data Structures and Idioms in Prolog, Lisp, and Java". You can find some chapters of it on the author's webpage, here: https://www.cs.unm.edu/~luger/ https://www.cs.unm.edu/~luger/ (You can also find the whole book online for free but I'm not sure of the legality of that so I'm not linking to it). There's some examples of programs implemented in both Python and Prolog on Rosetta Code, too.