If I have a simple predicate in the database, is there a way that I can request that the results be returned in a particular order (asc/desc) without having to read them all into memory and performing a sort/2 or doing a setof/3?
For instance, consider this database:
animal(dog).
animal(cat).
animal(elephant).
animal(bird).
animal(aardvark).
I would like to somehow declare animal as sorted ASC on its first term so that I can simple query it like:
?- animal(X).
X = aardvark ;
X = bird ;
X = cat ;
X = dog ;
X = elephant ;
No.
Being able to do that would be very convenient for treating my knowledge base more like a conventional database.
No, you cannot sort them if you dont read them.
Ofc you can write the predicates sorted or generate them sorted:
or
but you have to declare the predicate
animal/1as dynamic:(in the code file)
I used
compile_predicates/1to improve the speed; however, this means that you cannot useassert/1orretract/1again on that predicate so if you add/remove animals skip it.Alternatively you can use ordered sets and pass it as an argument