Implement takeWhile with fold
<p>How can I implement the takeWhile function in Haskell using fold?</p>

<pre><code>takeWhile :: (a -&gt; Bool) -&gt; [a] -&gt; [a]
</code></pre>

<p>I tried a strategy similiar to implementing filter like this</p>

<pre><code>filter :: (a -&gt; Bool) -&gt; [a] -&gt; [a]
filter f = foldr (\x acc -&gt; if f x then x : acc else acc) []
</code></pre>

<p>But how can I stop when f x is false?</p>