[Haskell-beginners] Re: Parsing a file with data divided into
sections
Patrick LeBoutillier
patrick.leboutillier at gmail.com
Wed Feb 4 09:15:03 EST 2009
> Nice program, especially your use of function composition is good style.
Thanks, I must admit I put alot of time into refactoring it. There's so many
different ways of doing stuff (and then simplifying them) in Haskell!
>> I think that you've pretty much used accumulators in the way they most
>> often are used. In many cases you don't _have_ to keep count though.
>> Here's one way to get the same result, but without keeping count:
>>
>> countDays [] = []
>> countDays ls = let
>> day = head ls
>> count = length $ takeWhile (isSpace . head) $ tail ls
>> in (day, count) : countDays (drop (1 + count) ls)
>>
>> main = interact (unlines . map show . countDays . lines)
>
> Here's a version using span from the Prelude:
>
> main = interact $ unlines . map show . countDays . lines
>
> countDays [] = []
> countDays (day:xs) = (day, length people) : countDays xs'
> where (people, xs') = span (isSpace . head) xs
>
Both of these examples are great and exactly what I was looking for: a
different approach to the problem. I guess the step that was missing in my
"thought process" is that recursion doesn't have to imply processing the list
elements 1 by 1 (1 recursive call for each element). Of course it makes
perfect sense once you see it....
Thanks a lot to everyone, as usual the people on this list are always very
kind and helpful.
Patrick
>
> Note that this file format is very simple and it's ok to use lines and
> isSpace to parse it. But the tool of choice are parser combinators
> like Text.Parsec or Text.ParserCombinators.ReadP .
>
>
> --
> http://apfelmus.nfshost.com
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
--
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada
More information about the Beginners
mailing list