开发者

Prolog problems

开发者 https://www.devze.com 2023-03-04 14:04 出处:网络
I use swi prolog. My code runs for first lin开发者_运维技巧e but it doesn\'t run the others. see(Data), //open file to read

I use swi prolog. My code runs for first lin开发者_运维技巧e 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消