
30 Sep
2006
30 Sep
'06
12:56 p.m.
On Sat, Sep 30, 2006 at 11:54:19AM -0400, Mark T.B. Carroll wrote:
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))
works better if you just skip the "head wraps" part. (and now i am curious: what was it supposed to mean? how did it get there?)
wrap :: Int -> String -> [String]
wrap _ "" = []
wrap width string = let (x, ys) = bestSplit width string in x : wrap width ys