title: How to force evaluation in Haskell?
body: <p>I am relatively new to Haskell and I am trying to learn how different actions can be executed in sequence using the do notation.
  In particular, I am writing a program to benchmark an algorithm (a function)</p>
  
  <pre><code>foo :: [String] -&gt; [String]
  </code></pre>
  
  <p>To this purpose I would like to write a function like</p>
  
  <pre><code>import System.CPUTime
  
  benchmark :: [String] -&gt; IO Integer
  benchmark inputList = do
                           start &lt;- getCPUTime
                           let r = foo inputList
                           end &lt;- getCPUTime
                           return (end - start) -- Possible conversion needed.
  </code></pre>
  
  <p>The last line might need a conversion (e.g. to milliseconds) but this is not the topic of this question.</p>
  
  <p>Is this the correct way to measure the time needed to compute function foo on some argument inputList?</p>
  
  <p>In other words, will the expression <code>foo inputList</code> be completely reduced before the action <code>end &lt;- getCPUTime</code> is executed? Or will <code>r</code> only be bound to the thunk <code>foo inputList</code>?</p>
  
  <p>More in general, how can I ensure that an expression is completely evaluated before some action is executed?</p>
  
  <hr>
  
  <p>This question was asked a few months ago on programmers (see <a href="https://softwareengineering.stackexchange.com/questions/160580/how-to-force-evaluation-in-haskell">here</a>) and had an accepted answer there but it has been closed as off-topic because it belongs on stack overflow. The question could not be moved to stack overflow because it is older than 60 days. So, in agreement with the moderators, I am reposting the question here and posting the accepted question myself because I think it contains some useful information.</p>