Note that there are some explanatory texts on larger screens.

plurals
  1. POHaskell: Equation Expander 1+(1+(1+(1+(…))))=∞
    primarykey
    data
    text
    <p>Does there exist a equation expander for Haskell?</p> <p>Something like <a href="http://foldr.com/" rel="nofollow noreferrer">foldr.com</a>: <code>1+(1+(1+(1+(…))))=∞</code></p> <p>I am new to Haskell I am having trouble understanding why certain equations are more preferable than others. I think it would help if I could see the equations expanded.</p> <p>For example I found <code>foldr</code> vs <code>foldl</code> difficult to understand at first until I saw them expanded.</p> <pre><code>foldr :: (a -&gt; b -&gt; b) -&gt; b -&gt; [a] -&gt; b foldr k z xs = go xs where go [] = z go (y:ys) = y `k` go ys foldl :: (a -&gt; b -&gt; a) -&gt; a -&gt; [b] -&gt; a foldl f z0 xs0 = lgo z0 xs0 where lgo z [] = z lgo z (x:xs) = lgo (f z x) xs </code></pre> <p>From the definitions I can see that <code>foldr</code> expands like this:</p> <pre><code>foldr (+) 0 [1..1000000] --&gt; 1 + (foldr (+) 0 [2..1000000]) --&gt; 1 + (2 + (foldr (+) 0 [3..1000000])) --&gt; 1 + (2 + (3 + (foldr (+) 0 [4..1000000]))) --&gt; 1 + (2 + (3 + (4 + (foldr (+) 0 [5..1000000])))) --&gt; </code></pre> <p>and <code>foldl</code> expands like this:</p> <pre><code>foldl (+) 0 [1..1000000] --&gt; foldl (+) (foldl (+) 0 [1]) [2..1000000]) --&gt; foldl (+) (foldl (+) (foldl (+) 0 [1])) [3..1000000]) --&gt; </code></pre> <p>or from <a href="http://www.haskell.org/haskellwiki/Foldr_Foldl_Foldl&#39;" rel="nofollow noreferrer">Haskell Wiki on foldr fold foldl'</a>:</p> <pre><code>let z1 = 0 + 1 in foldl (+) z1 [2..1000000] --&gt; let z1 = 0 + 1 z2 = z1 + 2 in foldl (+) z2 [3..1000000] --&gt; let z1 = 0 + 1 z2 = z1 + 2 z3 = z2 + 3 in foldl (+) z3 [4..1000000] --&gt; let z1 = 0 + 1 z2 = z1 + 2 z3 = z2 + 3 z4 = z3 + 4 in foldl (+) z4 [5..1000000] --&gt; </code></pre> <p>However, I have trouble on larger equations understanding why things work the way they do in Haskell. For example the first sieve function uses 1000 filters while the second sieve function takes only 24 to find the 1001 prime.</p> <pre><code>primes = sieve [2..] where sieve (p:xs) = p : sieve [x | x &lt;- xs, rem x p /= 0] primes = 2: 3: sieve (tail primes) [5,7..] where sieve (p:ps) xs = h ++ sieve ps [x | x &lt;- t, rem x p /= 0] -- or: filter ((/=0).(`rem`p)) t where (h,~(_:t)) = span (&lt; p*p) xs </code></pre> <p><a href="http://www.haskell.org/haskellwiki/Prime_numbers" rel="nofollow noreferrer">Haskell Wiki on Primes</a></p> <p>I have spent a good while working out and expanding by hand. I have come to understand how it works. However, an automated tool to expand certain expressions would greatly improve my understanding of Haskell.</p> <p>In addition I think it could also serve to help questions that seek to optimize Haskell code: </p> <ul> <li><a href="https://stackoverflow.com/questions/2915137/optimizing-haskell-code">Optimizing Haskell Code</a> </li> <li><a href="https://stackoverflow.com/questions/4378194/help-optimize-my-haskell-code-calculate-the-sum-of-all-the-primes-below-two-mil">Help optimize my haskell code - Calculate the sum of all the primes below two million</a></li> </ul> <p><strong>Is there a tool to expand Haskell expressions?</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload