Note that there are some explanatory texts on larger screens.

plurals
  1. POjson parsing in haskell part 2 - Non-exhaustive patterns in lambda
    primarykey
    data
    text
    <p>This is actually in continuation of the <a href="https://stackoverflow.com/q/17844223/1045053">question</a> I asked a few days back. I took the applicative functors route and made my own instances.</p> <p>I need to parse a huge number of json statements all in a file, one line after the other. An example json statement is something like this - </p> <pre><code>{"question_text": "How can NBC defend tape delaying the Olympics when everyone has Twitter?", "context_topic": {"followers": 21, "name": "NBC Coverage of the London Olympics (July &amp; August 2012)"}, "topics": [{"followers": 2705, "name": "NBC"},{"followers": 21, "name": "NBC Coverage of the London Olympics (July &amp; August 2012)"}, {"followers": 17828, "name": "Olympic Games"}, {"followers": 11955, "name": "2012 Summer Olympics in London"}], "question_key": "AAEAABORnPCiXO94q0oSDqfCuMJ2jh0ThsH2dHy4ATgigZ5J", "__ans__": true, "anonymous": false} </code></pre> <p><em>sorry for the json formatting. It got bad</em></p> <p>I have about 10000 such json statements and I need to parse them. The code I have written is something like this -</p> <pre><code>parseToRecord :: B.ByteString -&gt; Question parseToRecord bstr = (\(Ok x) -&gt; x) decodedObj where decodedObj = decode (B.unpack bstr) :: Result Question main :: IO() main = do -- my first line in the file tells how many json statements -- are there followed by a lot of other irrelevant info... ts &lt;- B.getContents &gt;&gt;= return . fst . fromJust . B.readInteger . head . B.lines json_text &lt;- B.getContents &gt;&gt;= return . tail . B.lines let training_data = take (fromIntegral ts) json_text let questions = map parseToRecord training_data print $ questions !! 8922 </code></pre> <p>This code gives me a runtime error <code>Non-exhaustive patterns in lambda</code>. The error references to <code>\(Ok x) -&gt; x</code> in the code. By hit and trial, I came to the conclusion that the program works ok till the 8921th index and fails on the 8922th iteration.</p> <p>I checked the corresponding json statement and tried to parse it standalone by calling the function on it and it works. However, it doesn't work when I call <em>map</em>. I don't really understand what is going on. Having learnt a little bit of haskell in "learn haskell for a great good", I wanted to dive into a real world programming project but seem to have got stuck here.</p> <p>EDIT :: complete code is as follows</p> <pre><code>{-# LANGUAGE BangPatterns #-} {-# OPTIONS_GHC -O2 -optc-O2 #-} {-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-} import qualified Data.ByteString.Lazy.Char8 as B import Data.Maybe import NLP.Tokenize import Control.Applicative import Control.Monad import Text.JSON data Topic = Topic { followers :: Integer, name :: String } deriving (Show) data Question = Question { question_text :: String, context_topic :: Topic, topics :: [Topic], question_key :: String, __ans__ :: Bool, anonymous :: Bool } deriving (Show) (!) :: (JSON a) =&gt; JSObject JSValue -&gt; String -&gt; Result a (!) = flip valFromObj instance JSON Topic where -- Keep the compiler quiet showJSON = undefined readJSON (JSObject obj) = Topic &lt;$&gt; obj ! "followers" &lt;*&gt; obj ! "name" readJSON _ = mzero instance JSON Question where -- Keep the compiler quiet showJSON = undefined readJSON (JSObject obj) = Question &lt;$&gt; obj ! "question_text" &lt;*&gt; obj ! "context_topic" &lt;*&gt; obj ! "topics" &lt;*&gt; obj ! "question_key" &lt;*&gt; obj ! "__ans__" &lt;*&gt; obj ! "anonymous" readJSON _ = mzero isAnswered (Question _ _ _ _ status _) = status isAnonymous (Question _ _ _ _ _ status) = status parseToRecord :: B.ByteString -&gt; Question parseToRecord bstr = handle decodedObj where handle (Ok k) = k handle (Error e) = error (e ++ "\n" ++ show bstr) decodedObj = decode (B.unpack bstr) :: Result Question --parseToRecord bstr = (\(Ok x) -&gt; x) decodedObj where decodedObj = decode (B.unpack bstr) :: Result Question main :: IO() main = do ts &lt;- B.getContents &gt;&gt;= return . fst . fromJust . B.readInteger . head . B.lines json_text &lt;- B.getContents &gt;&gt;= return . tail . B.lines let training_data = take (fromIntegral ts) json_text let questions = map parseToRecord training_data let correlation = foldr (\x acc -&gt; if (isAnonymous x == isAnswered x) then (fst acc + 1, snd acc + 1) else (fst acc, snd acc + 1)) (0,0) questions print $ fst correlation </code></pre> <p>here's the <a href="http://hr-testcases.s3.amazonaws.com/688/sample.zip" rel="nofollow noreferrer">data</a> which can be given as input to the executable. I'm using ghc 7.6.3. If the program name is ans.hs, I followed these steps.</p> <pre><code>$ ghc --make ans.hs $ ./ans &lt; path/to/the/file/sample/answered_data_10k.in </code></pre> <p>thanks a lot!</p>
    singulars
    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