Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using one of the Haskell parser libraries can make this kind of thing much less painful and error-prone. Here's an example of how to do this with <a href="http://hackage.haskell.org/package/attoparsec" rel="nofollow noreferrer">Attoparsec</a>:</p> <pre><code>module Main where import Control.Applicative import qualified Data.Map as M import Data.Attoparsec (maybeResult) import qualified Data.Attoparsec.Char8 as A import qualified Data.ByteString.Char8 as B type Environment = M.Map String String spaces = A.many $ A.char ' ' upTo delimiter = B.unpack &lt;$&gt; A.takeWhile (A.notInClass $ delimiter : " ") &lt;* (spaces &gt;&gt; A.char delimiter &gt;&gt; spaces) entry = (,) &lt;$&gt; upTo ':' &lt;*&gt; upTo ';' environment :: A.Parser Environment environment = M.fromList &lt;$&gt; A.sepBy entry A.endOfLine parseEnvironment :: B.ByteString -&gt; Maybe Environment parseEnvironment = maybeResult . flip A.feed B.empty . A.parse environment </code></pre> <p>If we have a file <code>context.txt</code>:</p> <pre><code>user: somebody; home: somewhere; x: 1; y: 2; z: 3; </code></pre> <p>We can test the parser as follows:</p> <pre><code>*Main&gt; Just env &lt;- parseEnvironment &lt;$&gt; B.readFile "context.txt" *Main&gt; print $ M.lookup "user" env Just "somebody" *Main&gt; print env fromList [("home","somewhere"),("user","somebody"),("x","1"),("y","2"),("z","3")] </code></pre> <p>Note that I'm using a <code>Map</code> to represent the environment, as camcann suggested in a comment on your previous <code>Reader</code> monad question.</p>
    singulars
    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.
    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