4 ms·
It's no different than: public class LinkedList { ... public int size() { if (head == null) { return 0; } else {
by kevhito 10y ago
It's no different than:
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.
- na85 10y agoWhy on earth doesn't LinkedList already have a size() method? I'm thinking of typed languages, I guess, but surely any sane collection type knows how to perform addition/concatenation with other instances of its own type, right?
- YeGoblynQueenne 10y agoProlog is a logic programming language. It's not object-oriented, so it doesn't have objects or methods. 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).
- saghm 10y agoThis is describing how you would implement LinkedList. Obviously, the size method has to be defined somewhere, and this is showing a potential implementation of it
- doublec 10y agoThere is a built in function for it in Prolog. But when it's presented as an example of how to implement it in the same way that list classes are often used as examples in OO languages even if they language implementation includes one.
- vidarh 10y agoThose collections have to be implemented by someone. You seem to be assuming that these collections spring fully formed into the world and are black boxes without an implementation.
- na85 10y agoI think you are willingly missing my point, because I believe it is obvious that I am not making that assumption.
- vidarh 10y agoLooking at the other responses to your comment, clearly it is not obvious that you're not making that assumption. If that's not what you were trying to say, then I still don't understand what you were trying to say.