Ayushi Agarwal
Ayushi Agarwal

Reputation: 31

how to assign one list to a variable in prolog?

I want to append([],C,C) where C is a list containing some elements . Is it possible? I will append some list in C containing elements append (Found,C,C) if other condition is true. And also i want to store final value in C to a variable D . How can I do that?

Upvotes: 2

Views: 9703

Answers (1)

Daniel Lyons
Daniel Lyons

Reputation: 22803

I want to append([],C,C) where C is a list containing some elements. Is it possible?

append([],C,C) is always true. An empty list combined with anything is that anything. Look what Prolog says when you attempt it:

?- append([],C,C).
true.

This true without any bindings tells you that Prolog established the proof but no new bindings were created as a result. This code would have the same result:

meaningless(_, _, _).

?- meaningless(everybody, X, Squant).
true.

This suggests your desire is misplaced. append([], C, C) does not do what you think it does.

I will append some list in C containing elements append (Found,C,C) if other condition is true. And also i want to store final value in C to a variable D. How can I do that?

Thinking in terms of "storing" and other operations implying mutable state is a sure sign that you are not understanding Prolog. In Prolog, you establish bindings (or assert facts into the dynamic store, which is a tar pit for beginners). Something similar could be achieved in a Prolog fashion by doing something like this:

frob(cat, List, Result) :- append([cat], List, Result).
frob(dog, List, List).

This predicate frob/3 has two in-parameters: an atom and a list. If the atom is cat then it will append [cat] to the beginning of the list. The threading you see going between the arguments in the head of the clause and their use in the body of the clause is how Prolog manages state. Basically, all state in Prolog is either in the call stack or in the dynamic store.

To give an example in Python, consider these two ways of implementing factorial:

def fac(n):
  result = 1
  while n > 1:
    result = result * n
    n = n - 1

This version has a variable, result, which is a kind of state. We mutate the state repeatedly in a loop to achieve the calculation. While the factorial function may be defined as fac(n) = n * fac(n-1), this implementation does not have fac(n-1) hiding in the code anywhere explicitly.

A recursive method would be:

def fac(n):
  if n < 1: 
    return 1
  else: 
    return n * fac(n-1)

There's no explicit state here, so how does the calculation work? The state is implicit, it's being carried on the stack. Procedural programmers tend to raise a skeptical eyebrow at recursion, but in Prolog, there is no such thing as an assignable so the first method cannot be used.

Back to frob/3, the condition is implicit on the first argument. The behavior is different in the body because in the first body, the third argument will be bound to the third argument of the append/3 call, which will unify with the list of the atom cat appended to the second argument List. In the second body, nothing special will happen and the third argument will be bound to the same value as the second argument. So if you were to call frob(Animal, List, Result), Result will be bound with cat at the front or not based on what Animal is.

Do not get mixed up and think that Prolog is just treating the last argument as a return value! If that were true, this would certainly not work like so:

?- frob(X, Y, [whale]).
X = dog,
Y = [whale].

What appears to have happened here is that Prolog could tell that because the list did not start with cat it was able to infer that X was dog. Good Prolog programmers aspire to maintain that illusion in their APIs, but all that really happened here is that Prolog entered the first rule, which expanded to append([cat], X, [whale]) and then unification failed because Prolog could not come up with an X which, having had [cat] prepended to it, would generate [whale]. As a result, it went to the second rule, which unifies X with dog and the second two arguments with each other. Hence Y = [whale].

I hope this helps!

Upvotes: 3

Related Questions