<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
<br>
Udo Stenzel wrote:
<blockquote cite="mid20061023213900.GA14959@web.de" type="cite">
  <pre wrap="">jim burton wrote:
  </pre>
  <blockquote type="cite">
    <pre wrap="">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 - 
    </pre>
  </blockquote>
  <pre wrap=""><!---->
fifths s = unwords.take 5.unfoldr (Just . splitAt l) $ s ++ repeat ' '
  where l = (length s + 4) `div` 5 
  </pre>
</blockquote>
<br>
<br>
Okay, you win.&nbsp; That's the nicest answer so far, I think.<br>
<br>
But here are solutions with a different theme altogether.&nbsp; They are
based on groupBy, not unfoldr. I really like the new `on` function.<br>
<br>
<br>
module Chunk where<br>
<br>
import Data.List<br>
<br>
(on) f g = \x y -&gt; f (g x) (g y)<br>
<br>
groupByIndex test xs =<br>
&nbsp;&nbsp;&nbsp; map (map snd) $ groupBy (test `on` fst) $ zip [0..] xs<br>
<br>
-- chunk : divide the input string into n chunks of equal length (len),
with padding<br>
<br>
-- chunk1 accepts the number of chunks<br>
chunk1 n pad xs = <br>
&nbsp;&nbsp;&nbsp; unwords $ take n $ groupByIndex ((==) `on` (`div` len)) $ xs ++
repeat pad<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where len = (length xs + n - 1) `div` n<br>
<br>
-- chunk2 accepts the length of each chunk<br>
chunk2 len pad xs = <br>
&nbsp;&nbsp;&nbsp; unwords $ take n $ groupByIndex ((==) `on` (`div` len)) $ xs ++
repeat pad<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where n = (length xs + len - 1) `div` len<br>
<br>
</body>
</html>