I`m writing a program on Python and Prolog, using the library PySwip. I need to send list from Python to Prolog. i did it like this:
from pyswip import Prolog
p = Prolog()
p.assertz("fruits([apple,banana,orange])") # Create list
p.assertz("result(X) :- fruits(L), member(X, L)") # Rule for checking if an element in a list
print(list(p.query("result(orange)."))) # Return True if element in list
But i need to make list a separate object, because execute some actions before question. Help me how to send object in Prolog
from pyswip import Prolog
p = Prolog()
fruits = [apple,banana,orange]
p.assertz("How send to list fruits ???") # I dont know how to write this request
Thank you everyone for advance
I don't know Prolog
and PySwip
and I don't know if I understand problem but it looks like normal string so you could convert to string using "fruit( {} )".format(fruits)
or using f-string f"fruit( {fruit} )"
from pyswip import Prolog
p = Prolog()
fruits = ['apple','banana','orange']
text = f"fruits({fruits})"
print(text)
p.assertz(text)
p.assertz("result(X) :- fruits(L), member(X, L)")
print(list(p.query("result(orange).")))
But in Python it has to be list with strings.
And it will create string fruits(['apple', 'banana', 'orange'])
with items in quotes ' '
.
I don't know if quotes makes some problem but if you need without quotes
fruits = ['apple','banana','orange']
text = ','.join(fruits) # apple,banana,orange
text = f"fruits([{text}])" # fruits([apple,banana,orange])
print(text)