delingma@THUNDER.LAKEHEADU.CA (04/27/91)
I am trying to get a function built that will terminate after reading some stuff followed by a carridge return. I can make it work for ANY other character, but not a CR. By putting in cuts al over the place, I managed to get it to almost work - but before the calling function dies, I need one more carridge return to end off the entire thing. If anyone has had a problem with this, or has some sample code to allow me to read in characters into a list that stops after getting a carridge return, I would reall appreciate it. ADVthanksANCE Dan Lingman delingma@thunder.lakeheadu.ca
imlah@canon.co.uk (Bill Imlah) (04/29/91)
delingma@THUNDER.LAKEHEADU.CA writes: >If anyone ...has some sample >code to allow me to read in characters into a list that stops >after getting a carridge return, I would really appreciate it. 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... read_line(L):- get_chars([],L). get_chars(IN,OUT):- get0(C), get_rest(C,IN,OUT). get_rest(10,L,L):- !. get_rest(C,IN,[C|OUT]):- get_chars(IN,OUT). -------------------------------------------------------- Bill Imlah imlah@canon.co.uk Canon Research Centre Europe, 17/20 Frederick Sanger Rd. The Surrey Research Park, Guildford, Surrey, GU2 5YD, UK. -- "If it ain't broke, don't fix it"
ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (04/30/91)
In article <9104262300.AA24421@thunder.LakeheadU.Ca>, delingma@THUNDER.LAKEHEADU.CA writes: > I am trying to get a function built that will terminate > after reading some stuff followed by a carridge return. > I can make it work for ANY other character, but not a CR. Why carriage returns (there is no d in "carriage")? What programming language are you using? What implementation of it? What operating system? Have you checked your manual to see what character or characters appears at the end of a line? If you are using a Prolog system on a PC, there is a good chance that lines are terminated by line feed characters (LF = 10) and that carriage returns are stripped out so that you never see them. (Note that the Common Lisp and Scheme definitions *demand* that lines appear to be terminated by a single #\Newline character, whatever that is.) Any good Prolog textbook will tell you how to read a line of characters. Mine does. -- Bad things happen periodically, and they're going to happen to somebody. Why not you? -- John Allen Paulos.
thom@ecrc.de (Thom Fruehwirth) (04/30/91)
Bill Imlah proposes > read_line(L):- > get_chars([],L). > > get_chars(IN,OUT):- > get0(C), > get_rest(C,IN,OUT). > > get_rest(10,L,L):- > !. > get_rest(C,IN,[C|OUT]):- > get_chars(IN,OUT). Observing that the first argument of get_chars/2 will be always bound to '[]' when called by read_line/1, we can remove this argument. As get_chars/2 is defined by a single clause, we can unfold it away: read_line(L):- get0(C), get_rest(C,L). get_rest(10,[]):- !. get_rest(C,[C|OUT]):- get0(C1), get_rest(C1,OUT). thom
ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (05/01/91)
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.