0
votes

Currently I am writing a Prolog program dealing with various different ways to get from one location to another and calculating the distances of these paths.

So far, I have been hard-coding my data clauses into my code as a quick work-around for testing purposes, but I need to write my code in such a way that a data file with the appropriately formatted data could be read into the program.

For example,

path(1, 4).
setRoutes([[1, 2, 45], [1, 3, 135],
           [2, 4,135], [3, 4, 45]] ).

will be the format of the inputted data, so I've hard-coded this into my program. The following isn't part of the input data, but I also hard-coded it for use in my program. How would I make the actual inputted information 'generic' while also making my own code 'generic' with respect to the original input?

linkedPoints(1, 2).
linkedPoints(1, 3).
linkedPoints(2, 4).
linkedPoints(3, 4).

My question is: how do I get from having to hard-code data into the program itself to creating "templates" that will read in user-inputted or file-inputted data?

Let me know if I need to clarify this at all... Thanks for the help!

1

1 Answers

1
votes

This is easy: You can use the built-in predicate read/1 to read a Prolog term from (for example) a file. So, as long as your data is represented as in your example, it makes little difference whether it is placed in the program itself or somewhere else, because Prolog terms can be processed easily from within Prolog.

AnotherThingThoughIsYourNamingConventionForExamplelinkedPoints/2: is_it_not_much_more_readable_to_use_underscores_like_linked_points/2?