Subject: Re: tree and _X

From: "Richard A. O'Keefe" <[email-masked]>
Date: Fri, 22 Sep 2000 15:08:06 +1200 (NZST)

What is wrong in this fragment of Prolog: ( Thinking in dictionary, that is
	a map of pairs Key -> Value)
	
	tree(Key, Value, Left, Right).

What is that line meant to do?

	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).
	
There are three problems here.
1.  No indentation.
2.  Some Prologs will realise this is (semi-)deterministic, but some won't.
3.  There is no clause that handles equality.

Best:

	lookup(Key, tree(K,V,L,R), Value) :-
	    compare(Ord, Key, K),
	    lookup_case(Ord, Key, Value, V, L, R).

	lookup_case(<, Key, Value, _, L, _) :-
	    lookup(Key, L, Value).
	lookup_case(>, Key, Value, _, _, R) :-
	    lookup(Key, R, Value).
	lookup_case(=, _, Value, Value, _, _).

	What is the difference between a variable wriiten as X and _X ?
	
This difference only:  if there is only one occurrences of a variable
in a clause, and that variable's name does NOT begin with an underscore,
Quintus and later Prologs will warn you that you have a "singleton
variable", because that's often the result of a spelling mistake.
There is no behavioural difference, and it is very bad style to use
leading underscores for variables that are intended to occur more than once.