title: Haskell: read a file by line
body: <p>I recently did the <a href="http://cemc.math.uwaterloo.ca/contests/computing.html">Waterloo CCC</a> and I feel that Haskell is the perfect language for answering these types of questions. I am still learning it. I am struggling a bit with the input, though.</p>
  
  <p>Here's what I'm using:</p>
  
  <pre><code>import IO
  import System.Environment
  import System.FilePath
  
  …
  
  main = do
      name &lt;- getProgName
      args &lt;- getArgs
      input &lt;- readFile $
          if not (null args)
              then head args
              else dropExtension name ++ ".in"
      let (k:code:_) = lines input
      putStrLn $ decode (read k) code
  </code></pre>
  
  <p>As you can see, I'm reading from the command-line given file path or from <code>j1.in</code> for example, if this program is called <code>j1.hs</code> and compiled to <code>j1</code>.</p>
  
  <p>I am only interested in the first two lines of the file, so I have used pattern matching to get those lines and bind them to <code>k</code> and <code>code</code>, in this example. And I then read <code>k</code> as an integer and pass it and the code string to my <code>decode</code> function, which I output.</p>
  
  <p>I'm wondering if <code>readFile</code> is loading the entire file into memory, which would be bad. But then I started thinking, maybe since Haskell is lazy, it only ever reads the first two lines because that's all it's asked for later on. Am I right?</p>
  
  <p>Also, if there is anything with that code sample that could be better or more idiomatic, please let me know.</p>