Last active
August 29, 2015 13:56
-
-
Save P-Seebauer/8802878 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Hey, I gave the things you did a little thought. | |
% But altered almost everything, that's why I don't think it's an sufficient answer anylonger so I made a gist. | |
% Firstof: I removed all your string construction with format and use one atom_codes | |
% (btw there is a string type in SWI-Prolog for efficient string handling). | |
% Next, I thought it should be part of the predicate if you end with a newline or had reached the end of file. | |
% After that, I put some neat little tricks in there to get backtracking behaviour for the get_line/3 - predicate. | |
% I hope this helps you in any way. | |
test(InFile) :- | |
open(InFile, read, InStrm), | |
get_line(InStrm,Line,How), | |
format('~s. String: ~s~n',[How,Line]). | |
% Offers other lines via backtracking | |
% (ignore the repeat line and everything after atom_codes if you only want one line) | |
get_line(InStrm,Line,How):- | |
repeat, | |
getline_list(InStrm,List,How), | |
atom_codes(Line,List), | |
(How = eof->!; | |
true). | |
getline_list(InStrm,Return,How):- | |
get_code(InStrm,Code), | |
getline_list_helper(InStrm,Code,Return,How). | |
% getline_list_helper(+Stream,+Character,-Return,?How) | |
getline_list_helper(_, C,[],How):- member((C,How),[(10,nl),(-1,eof)]),!. % terminates here | |
getline_list_helper(Strm,C ,[C|X],How):-getline_list(Strm,X,How). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment