I have some data in a file in the following format:
A)B
B)C
It describes nodes in a graph. The above would look like this if it were drawn out:
A -> B -> C
I want to write a Prolog program that can tell me if A can get to C, but I can’t seem to figure out how to open files, nor parse them into facts/relations.
I thought I could follow an example of how to read a single line from a file like this:
:- set_prolog_flag(verbose, silent).
:- initialization(main).
main :-
open('input/day06.example', read, Stream),
read(Stream, One),
close(Stream),
write([One]), nl,
halt.
main :- halt(1).
But this breaks because it seems that read/2
is only for reading from Prolog files.
How do I parse data of an arbitrary format in Prolog? In Python, you could do something like this, splitting the contents of a file on new lines and then split each line on your delimiter:
table = []
with open("input/day06.example") as f:
for line in f.read().strip().split("\n"):
left, right = line.split(")")
table.append((left, right))
Is it possible to do something similar in Prolog?
Thanks!