[Haskell-cafe] advice on a parsing function
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
>>> 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.
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
More information about the Haskell-Cafe