Prolog 读取文件时的一些问题

Prolog 读取文件时的一些问题,prolog,Prolog,我使用了一些prolog代码来读取要列出的文件号,读取正在工作,我不能使用包含已读取数字的列表 my_representation(Codes, Result) :- atom_codes(Result, Codes). stream_representations(Input, L) :- read_line_to_codes(Input, Line), ( Line == end_of_file -> L = [] ;write

我使用了一些prolog代码来读取要列出的文件号,读取正在工作,我不能使用包含已读取数字的列表

my_representation(Codes, Result) :-

    atom_codes(Result, Codes).

stream_representations(Input, L) :-

    read_line_to_codes(Input, Line),

    (   Line == end_of_file

    ->  L = []

    ;write("stream myrepresant oncesi Line="),writeln(Line),
    write("stream myrepresant oncesi FinalLine="),writeln(FinalLine),      
        my_representation(Line, FinalLine),
        stream_representations(Input, FurtherLines).

main :-
    stream_representations(Input, L),

    close(Input).


实际上,调用
stream_表示(Input,L)
将变量
L
实例化为atom
'1,2,3,4'
,如下查询所示:

?- my_representation([49, 44, 50, 44, 51, 44, 52], L).
L = '1,2,3,4'.
为了获得所需的结果,您可以修改谓词
my_表示法
,如下所示:

my_representation(Codes, Result) :-
    atom_codes(Atom0, Codes),                % obtain Atom0 = '1,2,3,4'
    format(atom(Atom1), '[~w]', Atom0),      % obtain Atom1 = '[1,2,3,4]'
    read_term_from_atom(Atom1, Result, []).  % transform atom '[1,2,3,4]' into list [1,2,3,4]
main :-
    open('test.txt', read, Input),
    stream_representations(Input, Codes),
    close(Input),
    my_representation(Codes, List),       % <= call new version only here
    writeln('list read': List),
    forall(append(Prefix, Suffix, List),
           writeln(Prefix - Suffix)).

stream_representations(Input, L) :-
    read_line_to_codes(Input, Line),
    (   Line == end_of_file
    ->  L = []
    ;   append(Line, FurtherLines, L),   % <= just append line to further lines
        stream_representations(Input, FurtherLines),
        writeln('Stream represention': L) ).

my_representation(Codes, Result) :-
    atom_codes(Atom0, Codes),
    format(atom(Atom1), '[~w]', Atom0),
    read_term_from_atom(Atom1, Result, []).
现在,我们有:

?- my_representation([49, 44, 50, 44, 51, 44, 52], L).
L = [1, 2, 3, 4].
[编辑]

您可以修改您的程序以使用新版本的谓词
my_表示法
,如下所示:

my_representation(Codes, Result) :-
    atom_codes(Atom0, Codes),                % obtain Atom0 = '1,2,3,4'
    format(atom(Atom1), '[~w]', Atom0),      % obtain Atom1 = '[1,2,3,4]'
    read_term_from_atom(Atom1, Result, []).  % transform atom '[1,2,3,4]' into list [1,2,3,4]
main :-
    open('test.txt', read, Input),
    stream_representations(Input, Codes),
    close(Input),
    my_representation(Codes, List),       % <= call new version only here
    writeln('list read': List),
    forall(append(Prefix, Suffix, List),
           writeln(Prefix - Suffix)).

stream_representations(Input, L) :-
    read_line_to_codes(Input, Line),
    (   Line == end_of_file
    ->  L = []
    ;   append(Line, FurtherLines, L),   % <= just append line to further lines
        stream_representations(Input, FurtherLines),
        writeln('Stream represention': L) ).

my_representation(Codes, Result) :-
    atom_codes(Atom0, Codes),
    format(atom(Atom1), '[~w]', Atom0),
    read_term_from_atom(Atom1, Result, []).

不起作用,它是converts,[[1,2,3,4]],但我需要[1,2,3,4],@Smsksrkn事实上,谓词
my_表示法
的拟议新版本工作正常!但是,当然,您需要修改您的程序以正确使用它。请参阅我答案中的“编辑”,了解如何进行编辑!