title: find last element of a list in haskell
body: <p>I wrote the following code to find the last element of a list in haskell:</p>
  
  <pre><code>myLast (x:xs) = do
      ret &lt;- if xs == [] then x else (myLast xs)
      return ret
  </code></pre>
  
  <p>The idea is to traverse the list until we are at an element which has the empty list as its next element. When we find it we set <code>ret</code> to that element.
  It makes sense for me but when I run the code inside the interactive shell I get the following error:</p>
  
  <pre><code>&lt;interactive&gt;:1:1: error:
      • No instance for (Num (m0 b0)) arising from a use of ‘it’
      • In a stmt of an interactive GHCi command: print it
  </code></pre>
  
  <p><strong>edit 1</strong></p>
  
  <p>The reason I used <code>do</code> was because I saw that pattern being used somewhere  to also traverse a list, so I thought I could do the same here. I'am avoiding libraries for now to get comfortable with the language.
  I wrote the function avoiding the do keyword and now it works:</p>
  
  <pre><code>myLast(x:xs) = if xs == [] then x else (myLast xs)
  </code></pre>
  
  <p>There's now just an issue with the empty list case. How to approach this in haskell?</p>