
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. Thanks in advance for any idea. Luca _________________________________________________________________ Download Messenger onto your mobile for free http://clk.atdmt.com/UKM/go/174426567/direct/01/

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

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@web.de To: beginners@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@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/

On Friday 30 October 2009 07:40:13 am Luca Ciciriello wrote:
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"]]
A variety of solutions on these blog posts, and the comments: http://gimbo.org.uk/blog/2007/04/20/splitting-a-string-in-haskell/ http://julipedia.blogspot.com/2006/08/split-function-in-haskell.html Shawn.

On Fri, Oct 30, 2009 at 01:40:13PM +0000, Luca Ciciriello wrote:
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"]]
Another possibility: import Data.List.Split -- from the 'split' package on Hackage str2lsts = map (splitOn ",") . splitOn "@" . filter (/='"') -Brent
participants (4)
-
Brent Yorgey
-
Daniel Fischer
-
Luca Ciciriello
-
Shawn Willden