% From the book % PROLOG PROGRAMMING IN DEPTH % by Michael A. Covington, Donald Nute, and Andre Vellino % (Prentice Hall, 1997). % Copyright 1997 Prentice-Hall, Inc. % For educational use only % File READSTR.PL % Reading and writing lines of text % Uses get0, and works in almost all Prologs (not Arity). % read_str(-String) % Accepts a whole line of input as a string (list of ASCII codes). % Assumes that the keyboard is buffered. read_str(String) :- get0(Char), read_str_aux(Char,String). read_str_aux(-1,[]) :- !. % end of file read_str_aux(10,[]) :- !. % end of line (UNIX) read_str_aux(13,[]) :- !. % end of line (DOS) read_str_aux(Char,[Char|Rest]) :- read_str(Rest). % read_atom(-Atom) % Reads a line of input and converts it to an atom. % See text concerning name/2 vs. atom_codes/2. read_atom(Atom) :- read_str(String), name(Atom,String). % or preferably atom_codes(Atom,String). % read_num(-Number) % Reads a line of input and converts it to a number. % See text concerning name/2 vs. number_codes/2. read_num(Atom) :- read_str(String), name(Atom,String). % or preferably number_codes(Atom,String). % write_str(+String) % Outputs the characters corresponding to a list of ASCII codes. write_str([Code|Rest]) :- put(Code), write_str(Rest). write_str([]).