Subject: Re: tree and _X

From: Nicos Angelopoulos <[email-masked]>
Date: Fri, 15 Sep 2000 13:17:29 +0100

On Fri, 15 Sep 2000, Waseem Besada wrote:

> What is wrong in this fragment of Prolog: ( Thinking in dictionary, that is
> a map of pairs Key -> Value)
> 
> tree(Key, Value, Left, Right).
> lookup(Key, tree(Key1, Value1, Left, Right), Value) :-
> Key < Key1, lookup(Key, Left, Value).
> lookup(Key, tree(Key1, Value1, Left, Right), Value) :-
> Key > Key1, lookup(Key, Left, Value).
> 
> ?- Dict=tree(1, ko, _C, tree(2,katt,_B,_A)), lookup(1,Dict,V),
> lookup(2,Dict,W).
> 
> The answer is No while I should get (as I beleive):
> V= ko, W=katt.
> 
> Why ?
>
	hmmm 
	maybe because you dont have a clause that deals with the case
	where Key and Key1 are equal

	in order to get some answer back you have to have 
	a succesful computation. 
	which in your case you dont have; because the goal 
	(for example) lookup(1,Dict,V) can not be reduced to the empty
	goal.

	you could direct the computation to do so by a clause of the form

	lookup(Key, tree(Key,Value,Left,Right),Value).

	(which is a special form of a clause called a fact btw) 
 
	the clause says that 
	IF the Key you are looking for is the key of the current tree
	then the current value is the value we are looking for 
	(it says something more actualy but this should for now)

	another way to see the above clause is as its equivelant :

	lookup(Key, tree(Key,Value,Left,Right),Value) :- true.

> another question:
> 
> What is the difference between a variable wriiten as X and _X ?
> 
> Regards,
> Waseem
> 
> 

	_X is an anonymous variable which normaly is used to 
	signal to the compiler that s/he shouldnt worry about the
	fact that this variable appears only once in the current 
	clause

	for instance 

isnogood( Z ) :-
	isnosobad( X, Z ).

	will (normaly) raise a warning when compiled/consulted
	whereas if you replace X by _X the warning disappears 

nicos.