Code Comments
Programming Forum and web based access to our favorite programming groups.I have this code that will read a file and return a list of strings for me. This part works very nice. The issue is that after i have got the list i will get an error if i go "next". I'd expect the stream to be closed, hence not producing any errors. Any suggestions? I dislike posting large source code pieces so i've tried to strip it down. If i have omitted something vital i do appologize deeply. % here's the fun part goAheadList(File, WordList) :- open(File, read, OS), readList(OS, WordList), close(OS). readList(OS, []) :- at_end_of_stream(OS). readList(OS, WordList) :- readWord(OS, Word), readList(OS, WordRest), WordList = [Word|WordRest]. readWord(InStream,Chars) :- get0(InStream,Char), checkCharAndReadRest(Char,Chars,InStream ). checkCharAndReadRest(10,[],_) :- !. checkCharAndReadRest(32,[],_) :- !. checkCharAndReadRest(-1,[],_) :- !. checkCharAndReadRest(end_of_file,[],_) :- !. checkCharAndReadRest(Char,[Char|Chars],I nStream) :- get0(InStream,NextChar), checkCharAndReadRest(NextChar,Chars,InSt ream). -- Kindly Konrad --------------------------------------------------- May all spammers die an agonizing death; have no burial places; their souls be chased by demons in Gehenna from one room to another for all eternity and more. Sleep - thing used by ineffective people as a substitute for coffee Ambition - a poor excuse for not having enough sense to be lazy ---------------------------------------------------
Post Follow-up to this messageKonrad Den Ende <tmp1@viltersten.com> wrote: > The issue > is that after i have got the list i will get an error if i > go "next". I'd expect the stream to be closed, hence > not producing any errors. Any suggestions? > goAheadList(File, WordList) :- > open(File, read, OS), > readList(OS, WordList), > close(OS). > readList(OS, []) :- at_end_of_stream(OS). > readList(OS, WordList) :- > readWord(OS, Word), > readList(OS, WordRest), > WordList = [Word|WordRest]. the second rule for readList applies even if you are at the end of the stream. So upon backtracking it will attempt to read from a closed stream. Gj
Post Follow-up to this messagehow do you do that for integers instead of chars?
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.