best practice of casting in Haskell

Thanks for the suggestion, actually I am already using Parser - a csutom parser for learning purposes - so the complete code looks as the below :

newtype Parser a = P (String -> [(a,String)])

`-- "title;post;title2;post" -> ["title","post","title2","post"] break :: Char -> Parser [String] break val = (end >> return []) <|> (parseTill val >>= \c -> (break val) >>= \cs -> return (c:cs))

-- comments chunks :: [String] -> Maybe [(String,String,String)] chunks str = case str of [] -> Just[] (id:title:content:xs) -> ((id,title,content):) <$> (chunks xs) _ -> Nothing

-- comments chunksToArticles :: Maybe [(String,String,String)] -> Maybe [Article] chunksToArticles val = (map performChunk) <$> val

-- comments performChunk :: (String,String,String) -> Article performChunk (id,title,content) = Article (readMaybe id :: Maybe Int) title content

-- comments articlesLookup :: Maybe [Article] -> Maybe [(Maybe Int,Article)] articlesLookup val =map ((Article id title content) -> (id,Article id title content)) <$> val`

and the below code to read the file from file and return nothing if anything goes wrong :

getArticlesFromDB :: FilePath -> T.HandleIO (Maybe [T.Article]) getArticlesFromDB path = do handle <- H.openF path ReadMode str <- H.readF handle let result = P.parse ((P.chunksToArticles . P.chunks) <$> (P.break ';')) str return $ fst . head $ result

however, your solution is more concise and clear and it handles where did the parse fail

/r/haskell Thread Parent