% Prolog Skeleton for assign3
% compile with
% gplc --no-top-level MainA.pl
% produces executable assign3
%
% The following is a required directive when compiling. You
% may comment it out when using the interpreter though.
%
:- initialization(main).
%
% Our main predicate. You should extend this as needed.
% Right now it just reads input from stdin and breaks it
% into a list of lines from the file.
%
main :- getContents(Contents),
split(Contents,List),
writelist(List).
%
% The predicates below are useful for input and output.
%
/* Display a List of atoms on a line */
writelist([]):- nl.
writelist([First|Rest]) :- write(First), writelist(Rest).
/* Character based getContents */
getContents(Contents):- get_char(Char), getRest(Char,Contents).
getRest(end_of_file,[]) :- !.
getRest(Char,[Char|Rest]):- get_char(Next), getRest(Next,Rest).
/* Split */
split(String,List) :- split(String,[],List).
/* The actual work that split does, is here in split/3 */
split([],[],[]).
split([],Line,[Line]).
split(['\n'|Tail],[],Lines) :- split(Tail,[],Lines).
split(['\n'|Tail],Line,[Line|Lines]):- split(Tail,[],Lines).
split([Head|Tail],Line,Lines) :- append(Line,[Head],NewLine), split(Tail,NewLine,Lines).