Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!wuarchive!uunet!munnari.oz.au!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.prolog Subject: Re: why do i need an extra carridge return? Message-ID: <5481@goanna.cs.rmit.oz.au> Date: 1 May 91 05:51:35 GMT References: <9104262300.AA24421@thunder.LakeheadU.Ca> <1991Apr29.102537.12322@canon.co.uk> Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 35 In article <1991Apr29.102537.12322@canon.co.uk>, imlah@canon.co.uk (Bill Imlah) writes: > Here is some working code. Prolog gurus: I'm a mere user so if > you're going to flame it, flame it in a nice way, as Dame Edna > would say... The code keeps on passing around a rather pointless []. Worse, it is not steadfast. We want a definition which says "get_chars(Chars) reads a line of characters from the current input stream then succeeds if Chars unifies with those characters." Improved code after the "%"s. > read_line(L):- % get_chars(Chars) :- > get_chars([],L). % get0(C), % get_chars_1(C, Cs), % Chars = Cs. % do this LAST, be steadfast > get_chars(IN,OUT):- > get0(C), > get_rest(C,IN,OUT). % get_chars_1(-1, _) :- !, fail. > % % do _something_ to handle end of file. % % you may have a better idea. > get_rest(10,L,L):- % get_chars_1(10, []) :- !. > !. % % change 10 to 13 on Macintoshes. > get_rest(C,IN,[C|OUT]):- % get_chars_1(C, [C|Cs]) :- > get_chars(IN,OUT). % get0(D), % get_chars_1(D, Cs). Quintus users, of course, already have suitable operations in library(lineio), which is described in the manual. -- Bad things happen periodically, and they're going to happen to somebody. Why not you? -- John Allen Paulos.