I fail to see the benefit... honestly it just seems like a waste of time.
I fail to see the benefit... honestly it just seems like a waste of time.
This example is about a list. But you could be very well telling the system about relationships between car parts instead. Or git commits.
Gerrit provides an actual real world use case: https://gerrit-review.googlesource.com/Documentation/prolog-...
In general, any general purpose language worth it's salt will have substantial portions of its standard library written in that language, and in this regard, prolog meets the criteria for respectsbl languages
Frex, this is a list:
.(a, .(b, .(c, .(d, []))))
'[]' is an atom that stands for the empty list.Normally however we write lists like this:
[a,b,c,d]
Which is syntactic sugar used in the vast majority of Prolog code.The easiest way to think of it is that a list is a pattern, consisting of two square brackets enclosing any number of terms. You can't really call functions on it, but you can pass it as an argument to other predicates, and match it to other patterns.
It is built in. No need to write your own length predicate. The quoted example is an illustration of a possible implementation.
(Cue boring discussion of "built in" vs. "defined in the standard library".)
In Java you can declare a list "my_list" and then ask for its length as in "my_list.size()". You can do the same in Prolog. Given the above Prolog definitions, here's an example of computing a list's length:
| ?- list_length([13, 42], ListLength).
ListLength = 2
yes
| ?-
NOT VERY EXCITING.But wait, you can run your logic BACKWARD. You can say instead, I have a list of length 2 -- what's inside the list??????
| ?- list_length(MyList, 2).
MyList = [_,_] ?
yes
| ?-
So that's strange. Prolog just gave us the form of an entity whose list-length is 2. And because we didn't say ANYTHING MORE SPECIFIC about the elements of the list, Prolog just gave us two placeholders for the items in the list ... '_' and '_'. Java can't do that out-of-the-box -- you can't say a list is size 2 and then ask for all the possible lists back.In our backwards-running Prolog example, we might have attached other conditions to the elements of the list ... in which case Prolog would (possibly) not have returned placeholders for the elements of the list, but might have done further work to figure out proper values to put into the list to satisfy the further conditions we gave for them.
public class LinkedList {
...
public int size() {
if (head == null) {
return 0;
} else {
int n0 = 1 + head.size();
}
}
Actually, it is different. In prolog, you don't tell it how to compute an answer. You tell it what the answer is, and it figures out the rest.In the previous example, I'd phrase it more like this:
I am sure [] has 0 length. I would be sure [_:Ls] has N length, if I was sure N>0 and N=N0+1 and [Ls] has N0 length.
Notice I never really told prolog how it is supposed to go about figuring out that [Ls] has N0 length -- prolog might use recursion. Or there might be other facts which let it figure out the length of [Ls]. Prolog will try to find a way. For example, I might introduce a rule
list_length(join(a, b), N) :- N #= NA + NB, list_length(a, NA), list_length(b, NB)
Now prolog knows that when you join two lists of known length, the lengths can be summed, without actually counting the length of either list or the combined list. And actually prolog doesn't even know or care what "join" means here, it just knows that "join" will cause the lengths to get summed.
If you want to know the length of a list you can use the predicate length/2:
?- length([a,b,c,d], N).
N = 4.
The example above just shows you how length/2 works (albeit with some CLP operators that are not typical).Edit: also, lists are not a "collection type" or any type at all. They're predicates, like everything else in the language. See my comment below - the easiest way to think of them is as patterns (just like in regexes, only Turing complete).
If that's not what you were trying to say, then I still don't understand what you were trying to say.