Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use <a href="http://www.swi-prolog.org/pldoc/man?predicate=read_line_to_codes%2F2" rel="noreferrer"><code>read_line_to_codes/2</code></a> or <a href="http://www.swi-prolog.org/pldoc/man?predicate=read_line_to_codes%2F3" rel="noreferrer"><code>read_line_to_codes/3</code></a>. An example program which reads individual lines from <a href="http://en.wikipedia.org/wiki/Standard_streams#Standard_input_.28stdin.29" rel="noreferrer">stdin</a> and prints them to <a href="http://en.wikipedia.org/wiki/Standard_streams#Standard_output_.28stdout.29" rel="noreferrer">stdout</a> is the following:</p> <pre><code>read_lines([H|T]) :- read_line_to_codes(user_input, H), H \= end_of_file, read_lines(T). read_lines([]). write_lines([]). write_lines([H|T]) :- writef("%s\n", [H]), write_lines(T). main :- read_lines(X), write_lines(X). </code></pre> <p>(This uses <a href="http://www.swi-prolog.org/pldoc/man?predicate=writef%2F2" rel="noreferrer"><code>writef/2</code></a> for printing.) There are also <a href="http://www.swi-prolog.org/pldoc/doc_for?object=read_stream_to_codes%2f2" rel="noreferrer"><code>read_stream_to_codes/2</code></a> and <a href="http://www.swi-prolog.org/pldoc/doc_for?object=read_stream_to_codes%2f3" rel="noreferrer"><code>read_stream_to_codes/3</code></a>, which are not concerned with lines. The following code prints all input from stdin in one go to stdout:</p> <pre><code>main :- read_stream_to_codes(user_input, X), writef("%s", [X]). </code></pre> <p>Of course it's also possible to read from a file instead of stdin. For more, see the <a href="http://www.swi-prolog.org/pldoc/doc_for?object=section%282%2c%20%27A.20%27%2c%20swi%28%27%2fdoc%2fManual%2freadutil.html%27%29%29" rel="noreferrer"><code>readutil</code></a> library.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload