[Haskell-beginners] editing a list

Bastian Erdnüß earthnut at web.de
Sun Nov 7 00:40:33 EDT 2010


On Nov 2, 2010, at 21:10, Daniel Fischer wrote:

> On Saturday 30 October 2010 16:59:00, Jonathan Phillips wrote:
>> I have a list (more precisely a string) which I'm trying to recurse
>> through, editing sections as I come to it.  It's obvious how to do it
>> with a for loop, but I'm doing it all wrong in haskell, as this simple
>> task is running mind-numbingly slowly.

Maybe you should split the string into lines using 'lines' from Data.List, then divide each line into 23-4-rest chars somehow using 'splitAt', apply the "show ((read str :: Integer) + 1)" part, justify the answer, probably by appending four spaces and then taking the first four characters, put the 23-changed4-rest parts back together with ++, and finally join all the lines again with 'unlines'.

>        l | l < 4 -> s ++ "    " ++ addOneNum' xs (n+4)
>           -- guessing it's the same number of spaces for those

I suppose it shall justify the number to length 4.  I.e.

>> changeNo v (w:x:y:z:xs) n       = case
>> length(s@(show((read([w,x,y,z])::Integer) + 1))) of
>>        0 -> addOneNum (v ++ "    ") xs (n+4)
>>        1 -> addOneNum (v ++ s ++ "   ") xs (n+4)
>>        2 -> addOneNum (v ++ s ++ "  ") xs (n+4)
>>        3 -> addOneNum (v ++ s ++ " ") xs (n+4)
>>        4 -> addOneNum (v ++ s) xs (n+4)
>>        _ -> error "asdf"

Cheers,
Bastian


More information about the Beginners mailing list