4 ms·
I can definitely highly recommend Prolog for playing. I found it to be a mind-opener in the same way learning Scheme for the first time was a mind-opener. Progr
by Gene_Parmesan 7y ago
I can definitely highly recommend Prolog for playing. I found it to be a mind-opener in the same way learning Scheme for the first time was a mind-opener. Programming with predicates/relations is extremely powerful.
As a teaser, consider a predicate Concat(X, Y, Z). In Prolog, capitalized identifiers are considered as unbound variables. There aren't return values; instead, you compute via unification.
So we can approximate the idea of a list concat funtion by entering something like `Concat([1, 2], [3, 4], X).` The Prolog evaluator will answer that X must be [1, 2, 3, 4] in order for the predicate to hold true. Looks like a standard function with a quirky way to provide an out param.
But these are predicates, not functions. Concat's internal logic defines a logical relation among the various values. This logic holds no matter which values you leave unbound. So here are some other things you get for free:
`Concat(X, Y, [1, 2, 3, 4]).` -- this will, in turn, iterate through all possible values of X and Y that make this predicate true. Thus Prolog will give us X: [], Y: [1,2,3,4]; but also X: [1], Y: [2,3,4]; and so on.
`Concat(X, [3, 4], [1, 2, 3, 4]).` -- this will give us X: [1, 2]; alternatively, it will give us the answer 'no' (sort of equivalent to false) if [3, 4] is not some tail of the provided list.
`Concat([1, 2], [3, 4], [1, 2, 3, 4]).` -- will give us "yes", for true.
This is a simple example, but now perhaps the potential power of Prolog is more visible. Imagine the possibilities -- for instance, a constructed grammar can tell you not only if a given candidate sentence is valid, but it can also (in theory) generate every single valid sentence in turn.
- ljm 7y agoI've been feeling this out through the Adventures in Prolog book. It's also a fascinating insight into how SQL engines work and what it means to write declarative code. I've had an idea for a prolog side-project for a while, but just haven't learned enough yet to know how I could implement it. In my mind, reserving a slot in a calendar or otherwise finding the next best opportunity is a valid use-case.