see(filename).
where the filename instantiates to an atom that names an existing
file, note that for filenames with dots in the atom must be single
quoted:
see('testprog.pro').
All reads will now be from this file until another is selected. Using
see with the same filename again just reselects it, it does not, after
the first use, re-open it. To close a file (and then be able to
re-open if required) use:
seen(filename).
To select the standard input just use see(user) where user is a
special atom that names both the standard input and output. Selecting
files for output is very similar, use:
tell(ofile). % open or select file for output
... % write it
told(ofile). % to close an output file
_342.
.''. Like write read cannot be retried by
backtracking.
tab(N) where N instantiates to an
integer outputs N spaces on the selected output file. nl with no
arguments outputs one newline on the current output file.
.'') and print the largest.
findlargest(File) :-
see(File), read(First),
largest(First,First,L),
nl, write('largest = '), write(L), nl, seen.
largest('end_of_file',Max,Max) :- !.
largest(Last, OMax, Max) :- Last > OMax, read(Next),
largest(Next, Last, Max).
largest(Last, OMax, Max) :- Last <= OMax, read(Next),
largest(Next, OMax, Max).
get0(N) instantiates N to the code of the next character
from the input file. It always succeeds but cannot be re-attempted by
backtracking. At end of file it returns the code for the end of file
mark, usually ``control z'' ie. 26. Output is similar, the predicate is
put(N) where N instantiates to an integer and the character
corresponding to this integer in the character set is output. It
always succeeds except when it is backtracked to.
.'' and return a list of words,
punctation is ommitted. It is not a complete program.
get_sentence(WList) :- get0(C), getrest(C,WList).
getrest(46,[]) :- !. % 46 is a "."
getrest(C,[W|WList]) :-
letter(C), !,
getletters(C,Lets,NC), name(W,Lets),
getrest(NC,WList).
getrest(C,WList) :- get0(NC), getrest(NC,WList).
getletters(LastC,[LastC|CList],NextC) :-
letterorprime(LastC), !,
get0(C), getletters(C,CList,NextC).
getletters(LastC,[],LastC).
letterorprime(39) :- !. % single quote
letterorprime(C) :- letter(C).
letter(C) :- 65 =< C, C =< 90.
letter(C) :- 97 =< C, C =< 122.
© University of Hertfordshire Higher Education Corporation (1998)