title: I need to compute as many times as remaining elements in the state in a State Monad
body: <p>I am studying this example of a stack implemented with the State monad. It uses the state monad implementation in scalaz.</p>
  
  <p>The example has been taken from Eugene Yokota post <a href="http://eed3si9n.com/learning-scalaz/State.html" rel="nofollow">http://eed3si9n.com/learning-scalaz/State.html</a></p>
  
  <pre class="lang-scala prettyprint-override"><code>  import scalaz._
    import scalaz.Scalaz._
  
    type Stack = List[Int]
  
    val pop = State[Stack, Int] {
      case x :: xs =&gt; (xs, x)
    }
  
    def push(a: Int) = State[Stack, Unit] {
      case xs =&gt; (a :: xs, ())
    }
  
    def stackManip: State[Stack, Int] = for {
      _ &lt;- push(3)
      a &lt;- pop
      b &lt;- pop
    } yield(b)
  
  
    val res: (Stack, Int) = stackManip(List(5, 8, 2, 1))
  </code></pre>
  
  <p>I've been trying to implement a piece of code to empty the stack in a functional fashion. The definition of  <code>stackManip</code> uses <code>pop</code> two times but what if I want the number of pop to be the same as the number of elements in the state (<code>List[Int]</code>)?</p>
  
  <p>The very first question to pop up would be "Why would you need that?" The answer is that I'm trying to implement a very similar case in which I need to execute pop as many times as elements in the statt. If someone can help me with the stack example then I'd be able to implement my particular case.</p>