[Haskell-beginners] beginner question
Luca Ciciriello
luca_ciciriello at hotmail.com
Fri Oct 30 11:51:46 EDT 2009
Thanks.
import Text.ParserCombinators.Parsec
simple = between (char '"') (char '"') (many (satisfy (/= '"')))
multiple = sepBy simple (char ',')
total = sepBy multiple (char '@')
str2lsts :: String -> [[String]]
str2lsts str= case parse total "" str of
Left err -> error (show err)
Right lsts -> lsts
solves my problem. As you can see I've replaced '§' with '@' and now all works fine.
Luca.
> From: daniel.is.fischer at web.de
> To: beginners at haskell.org
> Subject: Re: [Haskell-beginners] beginner question
> Date: Fri, 30 Oct 2009 15:46:33 +0100
>
> Am Freitag 30 Oktober 2009 14:40:13 schrieb Luca Ciciriello:
> > Hi all.
> >
> > Just a very basic question.
> >
> >
> >
> > I need to write a function str2lsts :: String -> [[String]] in order to
> > transorm a string like:
> >
> >
> >
> > "\"1\",\"cat\",\"dog\"§\"2\",\"duck\",\"goose\""
> >
> >
> >
> > in the list of lists:
> >
> >
> >
> > [["1","cat","dog"],["2","duck","goose"]]
> >
> >
> >
> > I've tried to mix recursion, pattern matching and list comprehension, but
> > the obtained result was embarrassing complex (> 20 lines of awful code). I
> > think that a more simple solution certainly exists.
> >
>
> splitOnToken :: Eq a => a -> [a] -> [[a]]
> splitOnToken t xs
> = case break (== t) xs of
> (hd,tl) -> hd:case tl of
> (_:r@(_:_)) -> splitOnToken t r
> _ -> []
>
> str2lsts = map (map read . splitOnToken ',') . splitOnToken '§'
>
> if things weren't enclosed in quotation marks inside the string, it would be the nicer
>
> map (splitOnToken ',') . splitOnToken '§'
>
> , provided of course, neither ',' nor '§' are valid characters for the target strings.
>
> import Text.ParserCombinators.Parsec
>
> simple = between (char '"') (char '"') (many (staisfy (/= '"')))
> -- alternative: simple = char '"' >> manyTill anyChar (char '"')
>
> multiple = sepBy simple (char ',')
>
> total = sepBy multiple (char '§')
>
> str2lsts str
> = case parse total "" str of
> Left err -> error (show err)
> Right lsts -> lsts
>
> >
> >
> >
> > Thanks in advance for any idea.
> >
> >
> >
> > Luca
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
_________________________________________________________________
Download Messenger onto your mobile for free
http://clk.atdmt.com/UKM/go/174426567/direct/01/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20091030/18dff5cb/attachment.html
More information about the Beginners
mailing list