3 ms·
In a sense, if you already understand functional programming, then the leap to logic programming is comparatively straight-forward: Say you have a function f w
by triska 9y ago
In 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.