[Haskell-cafe] split string into n parts

Bulat Ziganshin bulat.ziganshin at gmail.com
Mon Oct 23 15:52:54 EDT 2006


Hello jim,

Monday, October 23, 2006, 11:29:07 PM, you wrote:

> I want to split a string into 5 parts of equal length, with the last fifth
> padded if necessary, but can't get it right - here's what I've got - 

>> fifths :: String -> String
>> fifths s = unwords [a1,a2,a3,a4,a5]
>>     where l = (length s) `div` 5
             s0 = s++"        "
             (a1,s1) = splitAt l s0
             (a2,s2) = splitAt l s1
             ...

you can also use the following func:

splitByLen (len:lens) list  =  (x:splitByLens lens xs)    where (x,xs) = splitAt len list
splitByLen []         []    =  []

             

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



More information about the Haskell-Cafe mailing list