title: Some doubts about the reading of a file in Prolog using a Stream
body: <p>I have a doubt about how work this simple Prolog Program that read character from a file and put these character into a list.</p>
  
  <p>The code work well and is the following one:</p>
  
  <pre><code>readFile(FileInput, TextList):- open(FileInput, read, Stream),
                                  readCharacter(Stream, TextList),
                                  close(Stream),
                                  !.
  
  
  readCharacter(Stream,[]):- at_end_of_stream(Stream).    % Exit condition
  
  
  readCharacter(Stream,[Char|Tail]):- get0(Stream,Char),
                                      readCharacter(Stream,Tail).
  </code></pre>
  
  <p>When <strong>readFile/2</strong> predicate is call happens that: the input passed file is open in read mode and that file is associated with a stream.</p>
  
  <p>Then call the <strong>readCharacter/2</strong> predicate that take the Stream associated to my input file (the file to read) and the <strong>TextList</strong> that is the list where put the file content.</p>
  
  <p><strong>readCharacter/2</strong> predicate it is divided int two cases:</p>
  
  <p>1) An exit condition that use <strong>at_end_of_stream(Stream)</strong> predicate that succeeds after the last character of the Stream (so the last character in the file) is read. In this case do nothing and the <strong>readCharacter</strong> predicate end</p>
  
  <p>2) The second case is this rule that add a read character and ad it to my <strong>TextList</strong>:</p>
  
  <pre><code>readCharacter(Stream,[Char|Tail]):- get0(Stream,Char),
                                      readCharacter(Stream,Tail).
  </code></pre>
  
  <p>And here I have a doubt: it read a character from the Stream using <strong>get0</strong> and put it in the head of the list (as firs element of the list)...so...why the list don't contain the reverse of the file content?</p>
  
  <p>i.e.</p>
  
  <p>I have a file called <strong>myFile.txt</strong> that contains this text: <strong>abc</strong></p>
  
  <p>So <strong>readCharacter</strong> first read <strong>a</strong> character from the Stream and put it in the head of the list so I have:</p>
  
  <p><strong>TextList = [a]</strong></p>
  
  <p>Then <strong>readCharacter</strong> read <strong>b</strong> character from the Stream and put it in the head of the list si I have:</p>
  
  <p><strong>TextList = [b,a]</strong></p>
  
  <p>Then <strong>readCharacter</strong> read <strong>c</strong> character from the Stream and put it in the head of the list si I have:</p>
  
  <p><strong>TextList = [c,b,a]</strong></p>
  
  <p>If so TextList should contain the reverse content of myFile.txt file but it seems to me that TextList contain the same content of myFile.txt file in the same order.</p>
  
  <p>Why? What am I missing? What is wrong in my reasoning?</p>