free

free/1 (i)

This predicate succeeds if the supplied argument is a free Prova variable.

The following example test010.prova shows how the free predicate can be used in th eprocess of constructing a sorted tree representation of an initially unsorted list.

% Sort the list provided
:- solve(sort([7,3,6,1,4,5],L)).

insert_a(Val,t(Val,_,_)):-!.
insert_a(Val,t(Val1,Tree,_)):-
	Val<Val1,
	!,
	insert_a(Val,Tree).
insert_a(Val,t(_,_,Tree)):-
	insert_a(Val,Tree).

instree([],_).
instree([H|T],Tree):-
	insert_a(H,Tree),
	instree(T,Tree).

treemembers(_,T):-
	free(T),
	!,
	fail().
treemembers(X,t(_,L,_)):-
	treemembers(X,L).
treemembers(X,t(X,_,_)).
treemembers(X,t(_,_,R)):-
	treemembers(X,R).

sort(L,L1):-
	instree(L,Tree),
	findall(X,treemembers(X,Tree),L1).

This returns:

L=[1, 3, 4, 5, 6, 7]
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.