0

I'm a new comer to Prolog, and I don't understand how to do this in my exercise:

If I use the following command in the SWI-prolog, it will show the special vars like _G373.

14 ?- write([[_,_,_,_],[_,_,_,#],[_,_,_,_]]). 
[[_G403,_G406,_G409,_G412],[_G418,_G421,_G424,#],[_G433,_G436,_G439,_G442]]
true.

But in .pl files, if I read the same list of list from a file and store them in Puzzle,

read_file(Filename, Content) :-                     %read file using read lines
    open(Filename, read, Stream),                   %open a file, start read stream
    read_lines(Stream, Content),                    %using readlines store content from stream into content
    close(Stream).                                  %Close the stream

read_lines(Stream, Content) :-                      %read lines from read a single line
    read_line(Stream, Line, Last),                  %using read_line
    (   Last = true                                 %if last = ture, means read to the file end, make line = [], content = []
    ->  (   Line = []                               %else make content = [line]
        ->  Content = []
        ;   Content = [Line]
        )
    ;  Content = [Line|Content1],                   %store the lines from up to bottom
        read_lines(Stream, Content1)
    ).
read_line(Stream, Line, Last) :-                    %read line from read a character
    get_char(Stream, Char),                         %
    (   Char = end_of_file                          %if read to the file end, make line = [], last = true
    ->  Line = [],                                  %else if read to the line's end, make line[], last = false
        Last = true
    ; Char = '\n'
    ->  Line = [],
        Last = false
    ;   Line = [Char|Line1],
        read_line(Stream, Line1, Last)
    ).                                              %else store the character in order, just for one line

when I use write('Puzzle'),nl,write(Puzzle),nl., it only shows

Puzzle
[[_,_,_,_],[_,_,_,#],[_,_,_,_]]

How can I change these _ into special vars like the SWI-Prolog?

Thanks.

false
  • 10,264
  • 13
  • 101
  • 209
Lawrence
  • 1
  • 2
  • 1
    It seems you are confusing character-wise reading which results in a list of characters (that's what your read-file does) and reading Prolog syntax. See [this](http://stackoverflow.com/a/7948525/772868) if you are interested in reading Prolog syntax and want to keep specific variable names. – false Oct 13 '16 at 10:02

1 Answers1

1

Not very clear what and why you are trying to achieve. You can use read_term to read valid Prolog term. So, ff you actually have a file with the following contents, named test.pl:

[[_,_,_,_],[_,_,_,#],[_,_,_,_]].

Then, from the top level, you can do:

?- open('test.pl', read, In), read_term(In, T, []), close(In).
In = <stream>(0x1c71e10),
T = [[_4826, _4832, _4838, _4844], [_4856, _4862, _4868, #], [_4886, _4892, _4898, _4904]].

which seems to be what you are after?

Note that there was a full stop after the list in the file you are reading from.