Reputation: 515
Thinking of creating a database in Prolog, using a list seems obviously ineffective. So what I'd like to know is if it's possible somehow to get access to elements by their index or not and how, theoretically, a large database could be made.
Another subject I'm curious about is how to implement a simple task like that:
Assume we have 2 arrays
A [1,3,5,2,6,4] and B ["a","b","d","e","c","f"]
The goal is to bind letters to numbers and then sort both arrays to get
A [1,2,3,4,5,6] and B ["a","e","b","f","d","c"]
Upvotes: 1
Views: 144
Reputation: 40768
Regarding the database question: It is very common to use Prolog facts to store data. SWI-Prolog and other systems automatically build dynamic indices to support fast access to such collections. Regarding the second question, use for example keysort/2
on the term [1-"a",3-"b",...,4-"f"]. See pairs_keys_values/3 and related predicates in library(pairs).
Upvotes: 3
Reputation: 49873
For the index, you'd probably be better off representing the list as a collection of facts, something like isAtIndex( index, valueAtIndex )
for each element. Presumably, then, Prolog would use whatever indexing mechanism it has to match the appropriate fact you want (that is, ask it to prove isAtIndex(3,Value)
and it will bind Value to the matching value).
Similarly for your second question: you'd walk the 2 arrays in concert, asserting something like pair(valueFromA, valueFromB)
for each. Then you could create a sorted version of A, and you're all set.
Upvotes: 1