Haskell parse list of integers with Attoparsec

Haskell parse list of integers with Attoparsec

By : Aung Thu
Date : November 22 2020, 10:31 AM
hop of those help? I need some help parsing a list of comma delimited integers from a file, when the list of integers can be any length. The string might look like "1,2,3,4,5,6,7,8,..." and the list I need made would be like [1,2,3,4,5,6,7,8,...]. , You can use sepBy and decimal:
code :
parseTile :: Parser Tile
parseTile = do
  x <- decimal
  char ','
  y <- decimal
  char ':'
  t <- decimal `sepBy` (char ',')
  char ';'
  return $ Tile x y t

Share : facebook icon twitter icon
How do I parse a matrix of integers in Haskell?

How do I parse a matrix of integers in Haskell?

By : James Kiwuwa
Date : March 29 2020, 07:55 AM
wish help you to fix your issue So I've read the theory, now trying to parse a file in Haskell - but am not getting anywhere. This is just so weird... , The very simple version could be:
code :
import Control.Monad (liftM)

-- this operates purely on list of strings
-- and also will fail horribly when passed something that doesn't 
-- match the pattern
parse_lines :: [String] -> (Int, Int, [Int], [[Int]])
parse_lines (mn_line : ks_line : matrix_lines) = (m, n, ks, matrix)
    where [m, n] = read_ints    mn_line
          ks     = read_ints    ks_line
          matrix = parse_matrix matrix_lines

-- this here is to loop through remaining lines to form a matrix
parse_matrix :: [String] -> [[Int]]
parse_matrix lines = parse_matrix' lines []
    where parse_matrix' []       acc = reverse acc
          parse_matrix' (l : ls) acc = parse_matrix' ls $ (read_ints l) : acc

-- this here is to give proper signature for read
read_ints :: String -> [Int]
read_ints = map read . words

-- this reads the file contents and lifts the result into IO
parse_file :: FilePath -> IO (Int, Int, [Int], [[Int]])
parse_file filename = do
    file_lines <- (liftM lines . readFile) filename
    return $ parse_lines file_lines
*Main Control.Monad> parse_file "test.txt"
attoparsec or parsec in haskell

attoparsec or parsec in haskell

By : Abdo dahbi
Date : March 29 2020, 07:55 AM
wish of those help Parsec
Parsec is good for "user-facing" parsers: things where you have a bounded amount of input but error messages matter. It's not terribly fast, but if you have small inputs this shouldn't matter. For example, I would choose Parsec for virtually any programming language tools since--in absolute terms--even the largest source files are not that big but error messages really matter.
Haskell: How to use attoparsec in order to read a nested list from a ByteString

Haskell: How to use attoparsec in order to read a nested list from a ByteString

By : Sida Zhang
Date : March 29 2020, 07:55 AM
This might help you The data seems to be in JSON format, so you can use Data.Aeson decode function which works on ByteString
code :
import qualified Data.ByteString.Lazy as BL
import Data.Aeson
import Data.Maybe

main = do fContents <- BL.readFile filePath 
          let numList = decode fContents :: Maybe [[Int]]
          putStrLn (show $ fromJust numList)
How can I parse fixed-length, non-delimited integers with attoparsec?

How can I parse fixed-length, non-delimited integers with attoparsec?

By : user3764809
Date : March 29 2020, 07:55 AM
will help you Your code snippet is far from being self-contained (in particular, imports and the definition of your Packet data type are missing), but you seem to be overcomplicating things.
First, define a parser for one-digit integers. Then, use the latter as a building block for a parser for two-digit integers. After that, use applicative operators to combine those two parsers and define a parser for your custom Packet data type. See below.
code :
-- test_attoparsec.hs

{-# LANGUAGE OverloadedStrings #-}

import Control.Applicative ((<$>))
import Data.Attoparsec.Text
import Data.Char

data Packet = Std {-# UNPACK #-} !Int
                  {-# UNPACK #-} !Int
  deriving (Show)

stdK :: Parser Packet
stdK = char '1' *> (Std <$> twoDigitInt <*> oneDigitInt)

twoDigitInt :: Parser Int
twoDigitInt = timesTenPlus <$> oneDigitInt <*> oneDigitInt
    timesTenPlus x y = 10 * x + y

oneDigitInt :: Parser Int
oneDigitInt = digitToInt <$> digit
λ> :l test_attoparsec.hs
[1 of 1] Compiling Main             ( test_attoparsec.hs, interpreted )
Ok, modules loaded: Main.

λ> :set -XOverloadedStrings 

λ> parseOnly stdK "1341"
Right (Std 34 1)

λ> parseOnly stdK "212"
Left "1: Failed reading: satisfyWith"
Reading POT files in Haskell using Attoparsec

Reading POT files in Haskell using Attoparsec

By : Rahmat Andi Mulyana
Date : March 29 2020, 07:55 AM
To fix the issue you can do Assuming the POT file to read is stored in mypot.pot, you can use this code which you can directly run in runghc. I have tested this successfully using some Khan Academy PO files, but it currently only works with simple files (no plural etc), but it is easy to extend:
code :
{-# LANGUAGE OverloadedStrings #-}

import Prelude hiding (takeWhile)
import Data.Attoparsec.ByteString
import Data.Attoparsec.ByteString.Char8 hiding (takeWhile, skipWhile)
import qualified Data.Text.Encoding as TE
import Data.Text(Text)
import Data.Word
import Data.ByteString (unsnoc, ByteString)
import qualified Data.ByteString.Char8 as B
import Control.Applicative
import Control.Monad
import Data.Maybe
import Data.Either
import qualified Data.Text as T

data PORecord = PORecord {
        poComment :: Text,
        poMsgid :: Text,
        poMsgstr :: Text
    } deriving (Show, Eq)

takeTillEOL :: Parser ByteString
takeTillEOL = takeWhile (not . isEndOfLine)

parseMsgidMsgstrLine :: ByteString -> Parser Text
parseMsgidMsgstrLine key = do
  void (string key) <?> "Line key"
  char '"' <?> "Opening Qutotation mark"
  val <- takeTillEOL
  endOfLine <?> "EOL"
  return $ TE.decodeUtf8 $ fromMaybe "" $ (fst <$> unsnoc val)

msgidLine = parseMsgidMsgstrLine "msgid"
msgstrLine = parseMsgidMsgstrLine "msgstr"

escapedTextLine :: Parser Text
escapedTextLine = char '"' *> (TE.decodeUtf8 <$> takeTillEOL) <* endOfLine
--escapedTextLine = do
--    char '"'
--    val <- takeTillEOL
--    return $ (traceShow val ())

nameP :: String -> Parser a -> Parser a
nameP str p = p <?> str

commentLine :: Parser Text
commentLine = nameP "comment line" $ do
    char '#' <?> "Line start hash"
    -- Skip space but not newline
    void $ many (char ' ')
    txt <- TE.decodeUtf8 <$> takeTillEOL
    endOfLine <?> "EOF"
    return txt

emptyLine :: Parser ()
emptyLine = skipSpace <* endOfLine

poRecord :: Parser PORecord
poRecord = do
    comments <- many1 commentLine <?> "Comments"
    msgidPrimary <- msgidLine <?> "msgid"
    extraMsgid <- many escapedTextLine <?> "Extra msgid"
    msgstrPrimary <- msgstrLine <?> "msgstr"
    extraMsgstr <- many escapedTextLine <?> "Extra msgstr"
    let comment = T.intercalate "\n" comments
    let msgid = T.intercalate "\n" $ msgidPrimary : extraMsgid
    let msgstr = T.intercalate "\n" $ msgstrPrimary : extraMsgstr
    return $ PORecord comment msgid msgstr

poFile :: Parser [PORecord]
poFile =
    --let options = choice [emptyLine *> pure Nothing, ]
    many1 poRecord <?> "PO results"

main :: IO ()
main = do
    f <- B.readFile "mypot.pot"
    print $ parseOnly poFile f
Related Posts Related Posts :
  • Haskell Shelly sample code
  • Finding type signatures in Haskell that are too specific
  • haskell - will pattern matching work faster for non-specific form of an algebraic data type?
  • Cannot enter multiline statements in GHCi
  • Using Gloss to run a simulation while using SDL to play a sound
  • Haskell - Calculating the shortest path using trees
  • Beginner: Converting Types in Haskell
  • G-machine, (non-)strict contexts - why case expressions need special treatment
  • Indentation with Let Statements
  • Change (0, 1] to (0, 1) without branching
  • Haskell equivalent to contains
  • Is there a way to unmap in Haskell?
  • Using HLint.hs file in EclipseFP
  • Don't know how to use . and $ operator in Haskell
  • Why is my haskell code so slow
  • How is FRP handled in terms of memory?
  • Generalizing traversal of expressions with changes on specific nodes
  • Why does pattern matching on this GADT seem to introduce ambiguity in the type checker?
  • How to detect end of input with pipes
  • Basic Haskell IO Monad FilePath join
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org