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”]
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 proveisAtIndex(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.