I've been doing it as the enclosed. I wrote it a while ago, though, and
haven't really looked too hard at it since.
-- Mark
module WordWrap (wrap) where
import Data.Maybe
options :: String -> [(String, String)]
options [] = [("", "")]
options (x:xs) =
let rest = map (\(ys, zs) -> (x:ys, zs)) (options xs)
in if x == ' ' then ("", xs) : rest else rest
bestSplit :: Int -> String -> (String, String)
bestSplit width string =
last (head wraps : takeWhile ((<= width) . length . fst) (options string))
wrap :: Int -> String -> [String]
wrap _ "" = []
wrap width string =
let (x, ys) = bestSplit width string
in x : wrap width ys
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe