[Haskell-beginners] Re: mind your head, was: Parsing a file with data divided into sections

Magnus Therning magnus at therning.org
Fri Feb 6 04:52:28 EST 2009


On Fri, Feb 6, 2009 at 8:47 AM, Christian Maeder
<Christian.Maeder at dfki.de> wrote:
> I wrote:
>> Magnus Therning wrote:
>>>>         isSection line = not . isSpace . head $ line
>>
>> I'ld be more careful with "head"! Imagine your binary fails with:
>>
>> Prelude.head: empty list
>
> see also
> http://haskell.org/haskellwiki/Haskell_programming_tips#Partial_functions_like_fromJust_and_head
>
>>> countDays [] = []
>>> countDays ls = let
>>>         day = head ls
>>>         count = length $ takeWhile (isSpace . head) $ tail ls
>>>     in (day, count) : countDays (drop (1 + count) ls)
>>
>> here too, and better use a pattern like: ls@(hd, tl)
>
> oops, the pattern should be: ls@(hd : tl)

Another course of action would be to add exception handling; if head
throws an exception then the input file isn't following the accepted
syntax.

/M

-- 
Magnus Therning                        (OpenPGP: 0xAB4DFBA4)
magnus@therning.org          Jabber: magnus@therning.org
http://therning.org/magnus         identi.ca|twitter: magthe


More information about the Beginners mailing list