[Haskell-cafe] cutting long strings into lines

Andrea Rossato mailing_list at istitutocolli.org
Sat Sep 30 14:50:25 EDT 2006


On Sat, Sep 30, 2006 at 08:56:24PM +0400, Bulat Ziganshin wrote:
> splitByLen len_f [] = []
> splitByLen len_f xs = y : splitByLen len_f ys
>                        where (y,ys) = splitAt (len_f xs) xs
...
> so, "splitByLen len_f" should give you that you need, you need only to
> add checks for some additional conditions (first word in line is more
> than 80 bytes long, it is a last line) and removing of the extra space
> on each line

I came up with this solution that seem to be fine, to me. I does the
checking of those additional conditions:

findSplitP at = last . filter (<at) . findIndices (==' ')
                where last [] = at
                      last [x] = x
                      last (_:xs) = last xs

wrapLS at [] = []
wrapLS at s = take ln s ++ "\n" ++ rest 
    where ln = findSplitP at s
          remain = drop ln s
          rest = if length remain > at 
                 then wrapLS at (tail remain) 
                 else tail remain

then you can use lines/unlines to split it.

Thanks for your help.
Best regards,
Andrea


More information about the Haskell-Cafe mailing list