[Haskell-cafe] How would you replace a field in a CSV file?

Bulat Ziganshin bulat.ziganshin at gmail.com
Mon Oct 2 02:28:06 EDT 2006


Hello tpledger,

Monday, October 2, 2006, 3:11:29 AM, you wrote:

> For such a small self-contained task, I don't think Haskell
> is any better than Python.

i disagree. while it's hard to beat Python version in number of lines,
Haskell version may have the same length and better performance.

for this particular task, using list as intermediate datastructure
make program both long and inefficient. if ByteString will include
array-based splitting and joining, the things will become much better

main = B.interact $ B.unlines . map doline . B.lines
    where doline    = B.joinArray comma . mapElem 9 fixup . B.splitArray ','
          fixup s   = M.findWithDefault s s
          comma     = B.pack ","

mapElem n func arr = arr//[(n,func (arr!n))]


if mapElem, splitArray, joinArray will be library functions (i think
they are good candidates) this program will be not longer than Python
one


-- 
Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com



More information about the Haskell-Cafe mailing list