Department of Computer Science



next up previous
Next: A functional programming language: Up: A goal directed programming Previous: Control and Cut


Input and Output


Subsections

Files

None of the input and output predicates takes a file name as argument, they all use the currently selected input or output file. Initially the selected input is the terminal keyboard (standard input on Unix) and the selected output is the terminal screen (standard output on Unix). To open and select a file for input use:
      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

Writing terms

The predicate write takes one argument and always succeeds when tried but it has a side-effect aswell, it writes its argument term. If backtracking occurs it cannot be re-satisfied it will fail. If its argument is an instantiated variable it will write the value, if it is uninstantiated then an anonymous variable name is printed, eg. _342.

Reading terms

read with one uninstantiated variable argument will read a term and instantiate the variable to it. The form of term read is similar to the form of Prolog programs so a term must be followed by a dot ``.''. Like write read cannot be retried by backtracking.

Other Output Predicates

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.

Example

To read a file containing integers (each followed by a ``.'') 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).

Reading and Writing Characters

Prolog represents characters by small integers corresponding to their Character Code (ASCII) representation. So getting the character A would produce 65. The predicate 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.

Example Reading Characters

The following are some rules to input a sentence of text upto a full-stop ``.'' 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.


next up previous
Next: A functional programming language: Up: A goal directed programming Previous: Control and Cut


Page generated: 2002-11-04 by Bob Dickerson

© University of Hertfordshire Higher Education Corporation (1998)

Disclaimer