From nangelop@csd.abdn.ac.uk  Fri Sep 15 14:17:13 2000
Received: from pigeon.csd.abdn.ac.uk (root@pigeon.csd.abdn.ac.uk [139.133.200.15])
	by swi.psy.uva.nl (8.9.3/8.9.3) with ESMTP id OAA05899
	for <prolog@swi.psy.uva.nl>; Fri, 15 Sep 2000 14:17:13 +0200 (MET DST)
Received: from kea (nangelop@kea [139.133.200.24])
	by pigeon.csd.abdn.ac.uk (8.11.0/8.11.0) with ESMTP id e8FCHTF22482
	for <prolog@swi.psy.uva.nl>; Fri, 15 Sep 2000 13:17:30 +0100 (BST)
Date: Fri, 15 Sep 2000 13:17:29 +0100
From: Nicos Angelopoulos <nangelop@csd.abdn.ac.uk>
To: prolog@swi.psy.uva.nl
Subject: Re: tree and _X
Message-ID: <Pine.SGI.4.21.0009151317110.233195-100000@kea.csd.abdn.ac.uk>
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII



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.


