title: HW: Unfolding a list in a specific way
body: <p>In Programming in Haskell, Graham Hutton defines an unfold for lists as follows:</p>
  
  <pre><code>unfold :: (b -&gt; Bool ) -&gt; (b -&gt; a) -&gt; (b -&gt; b) -&gt; b -&gt; [a]
  unfold p h t x | p x = []
  | otherwise = h x : unfold p h t (t x)
  </code></pre>
  
  <p>Define a function</p>
  
  <pre><code>• listUnfold :: (b -&gt; Bool) -&gt; (b -&gt; a) -&gt; (b -&gt; b) -&gt; b -&gt; [a]
  </code></pre>
  
  <p>that is similar to the one above but uses unfoldr in its implementation and is non-recursive.</p>
  
  <p>I've been trying for a while to solve the question above but I still can manage to do so (pretty new in Haskell and functional programming in general).</p>
  
  <p>My attempt:</p>
  
  <pre><code>listUnfold :: (b -&gt; Bool) -&gt; (b -&gt; a) -&gt; (b -&gt; b) -&gt; b -&gt; [a]
  listUnfold f h t x 
      | f x == True   = []
      | otherwise     = h x : 
          listUnfoldr (\x -&gt; if f x then Nothing else Just ((h x), (t x))) x
  </code></pre>
  
  <p>In english, if <code>f x</code> is true, return the empty list. Otherwise, use <code>h x</code> as the head and append the results of unfoldr as the tail. Unfoldr takes a list <code>(x:xs)</code> should recurse itself with <code>x</code> as the head and <code>xs</code> as the tail. </p>
  
  <p>p/s: I'm probably doing this very very wrongly. </p>