Actually, the call stream_representations(Input, L)
instantiates the variable L
to the atom '1,2,3,4'
, as can be seen with the following query:
?- my_representation([49, 44, 50, 44, 51, 44, 52], L).
L = '1,2,3,4'.
In order to obtain the desired result, you could modify the predicate my_representation
as following:
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]
Now, we have:
?- my_representation([49, 44, 50, 44, 51, 44, 52], L).
L = [1, 2, 3, 4].
[EDIT]
You can modify your program to use this new version of the predicate my_representation
as following:
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, []).
Result:
?- main.
Stream represention:[49,44,50,44,51,44,52]
list read:[1,2,3,4]
[]-[1,2,3,4]
[1]-[2,3,4]
[1,2]-[3,4]
[1,2,3]-[4]
[1,2,3,4]-[]
true.