selda
selda

Reputation: 179

Prolog problems

I use swi prolog. My code runs for first line but it doesn't run the others.

 see(Data), //open file to read
   repeat,
    readln(A), //read line from file 
    write(A),
   A\==end_of_file,!.

I get an unexpected end of file error. Do you have any idea?

Upvotes: 0

Views: 452

Answers (1)

thanos
thanos

Reputation: 5858

readln/1 is not a in the swi-prolog manual so maybe you should include the code for that too. assuming that it does what you say, the code should probably be:

 see(Data), //open file to read
   repeat,
   readln(A), //read line from file 
   write(A),
   A=end_of_file,!.

the repeat/0 "structure" works like repeat...until

on a side note, i would prefer recursive solution...like

io(end_of_file):-
    write(end_of_file).
io(_):-
    readln(A),
    write(A),
    io(A).

feels more declarative.

Upvotes: 1

Related Questions