Is it possible to save variables in Prolog?
I would like to save and edit a list as the program runs but I cannot find anyway of saving the list between edits.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Probably the Prolog feature you want is assert/retract of a dynamic predicate like myListVars/1, which does nothing more than save a specified list of values that you can retrieve subsequently.
For example, if you had three variables
X,Y,Zthat you wanted to memorialize, they could be saved into a dynamic fact myListXYZ/1 with a predicate like this:and retrieved with a predicate like this:
This approach assumes you will only have (at most) one fact at a time for myListXYZ/1, which is guaranteed if one only accesses that predicate through the get/set pair above.
I often use similar “fact” predicates to store test cases for Prolog programs where it is inconvenient to type required arguments in manually. More than one test case can be accomodated by adding an extra argument that “labels” the test cases.