Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're writing a parsing function. The basic type would be something like <code>String -&gt; Tree Int</code>, but there are a few powerful refinements worth considering.</p> <p>First, not all <code>String</code>s are actually translatable into <code>Tree</code>s (i.e. <code>")"</code> is definitely not a tree) so it's better to refine the function to something with a type like <code>String -&gt; Maybe (Tree Int)</code>, the <code>Maybe</code> monad indicates possible failure.</p> <p>Secondly, you probably don't really want to reinvent an <code>Int</code> parser. You can use the <code>Read</code> typeclass to get access to a collection of parsers. So you might</p> <ul> <li>refine the type again then as <code>Read a =&gt; String -&gt; Maybe (Tree a)</code></li> <li>import the <code>Safe</code> module to get access to <code>readMay :: Read a =&gt; String -&gt; Maybe a</code></li> <li>and then just build the <code>Tree</code> parsing components.</li> </ul> <p>Thirdly, <code>Tree</code>s are highly recursive, your parenthetical Lispy grammar is highly recursive, your parser ought also to be. To this end, focus on just parsing a single node <code>"(...)"</code> and then recursively parsing the inner nodes as needed.</p> <p>Finally, you might want to eventually consider some even more powerful parsing tools. <code>Parsec</code> is much more powerful than the <code>Read</code> class and makes building more complex, recursive parsers from smaller, simpler pieces a snap.</p>
 

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