[Haskell-cafe] advice on a parsing function

minh thu noteed at gmail.com
Wed Mar 11 14:45:18 EDT 2009


2009/3/11 Manlio Perillo <manlio_perillo at libero.it>:
> Manlio Perillo ha scritto:
>>
>> minh thu ha scritto:
>>>
>>> [...]
>>> The approach I suggested is a bit overkill. You can indeed use L.lines
>>> to split the input into lines then work on that.
>>>
>>> But still, avoid the pair (Int, Bytestring). Instead, you can basically
>>> map
>>> on each line the unsafeReadInt modified to :
>>> - return the id
>>> - return if it is one kind of id or the other kind.
>>>
>>> so :
>>> type UserId = Int
>>> type MovieId = Int
>>> unsafeReadInt :: Line -> Either MovieId UserId
>>>
>>> Now you have a nice list [Either MovieId UserId] that
>>> you need to transform into (MovieId, [UserId]).
>>>
>>
>> Thanks, this seems a much better solution.
>>
>
> Done:
> http://hpaste.org/fastcgi/hpaste.fcgi/view?id=2309

One improvement you can do :

In the line
      quiz' (Left id : l) = (id, quiz'' l) : quiz' l

notice you use two times the 'l', and the next line of code
pass through the Right case.

Change your code so that quiz'' has a return type ([UserId],Bytestring).
The above line becomes

    quiz' (Left id : l) = (id, ids) : quiz' rest
    where (ids,rest) = quiz'' l

Thu


More information about the Haskell-Cafe mailing list