[Haskell-cafe] cutting long strings into lines

Matthias Fischmann fis at wiwi.hu-berlin.de
Sat Sep 30 12:56:43 EDT 2006


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20060930/8d186104/attachment-0001.bin


More information about the Haskell-Cafe mailing list