3 ms·
In Prolog you can "run your logic backwards". 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
by nwatson 10y ago
In Prolog you can "run your logic backwards".
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.