[Haskell-cafe] split string into n parts

Mark T.B. Carroll mark at ixod.org
Mon Oct 23 16:21:41 EDT 2006


jim burton <jim at sdf-eu.org> writes:
(snip)
> *Main> fifths "IDOLIKETOBEBESIDETHESEASIDE"
> "IDOLI KETOBE BESIDE THESEA SIDEXX"
> *Main> fifths "12345"
> "1 23 45"
(snip)

FWIW this unholy thing works for me,

fifths :: String -> String

fifths = splitIntoN 5

splitIntoN :: Int -> String -> String

splitIntoN n string = 
    let stringToSplit = string ++ replicate (n-1) 'X'
    in unwords (map fst (take n (tail (iterate (splitAt (div (length stringToSplit) n) . snd) (undefined, stringToSplit)))))

Admittedly, a 'let' might be nice to name some intermediate
computations.

-- Mark



More information about the Haskell-Cafe mailing list