title: Clojure - Side Effects Happening Out Of Order
body: <p>While dabbling in Clojure I've written a very basic program to echo whatever the user types into it. However, it doesn't run in a way that I'm perceiving to be natural. Here's the code:</p>
  
  <pre><code>(defn goo []
    (print "echo&gt; ")
    (def resp (read-line))
    (print resp)
  )
  </code></pre>
  
  <p>I would expect the code to run like this (for me typing in <code>foo</code> as the input to <code>read-line</code>):</p>
  
  <pre><code>user=&gt; (goo)
  echo&gt; foo
  foonil
  </code></pre>
  
  <p>But instead, the echo and read-line is switched:</p>
  
  <pre><code>user=&gt; (goo)
  foo
  echo&gt; foonil
  </code></pre>
  
  <p>Why does this happen? Is there a subtlety I'm missing?</p>
  
  <p>EDIT: From Joe's answer, the updated correct solution is:</p>
  
  <pre><code>(defn goo []
    (print "echo&gt; ")
    (flush)
    (def resp (read-line))
    (print resp)
    (flush)
  )
  </code></pre>
  
  <p>Also, the flushes aren't necessary if you use <code>println</code> instead of <code>print</code>.</p>