我试图将文本格式化为矩形的形状;目前我已经能够正确地得到它,但最后一行并没有尽可能延长。Haskell:递归问题
我试图计算最佳的字段宽度,以最大限度地减少或完全删除它。
我完全卡住了。下面的代码显示了相关的功能。目前它陷入了无限循环。 我哪里错了?
在附注中,调试Haskell代码的最佳方法是什么? (是的,我对此很新颖。)
optimalFieldWidth应该比较行长度,直到顶行的长度等于底行的长度,然后返回导致它的字段宽度真正。
module Main where
import System
import Data.List
main = do
(f:_) <- getArgs
xs <- getContents
putStr (show (bestFieldWidth maxLineLength xs))
bestFieldWidth :: Int -> String -> Int
bestFiledWidth _ [] = 0
bestFieldWidth lineLength xs
| length (last input) == length (head input) = lineLength
| otherwise = bestFieldWidth (length (head (rect (lineLength-1) xs))) xs
where input = lines xs
rect :: Int -> String -> [String]
rect _ [] = []
rect lineLength xs
| length input <= len = [input]
| otherwise = take len input : rect len (drop len input)
where input = trim xs
len = bestFieldWidth lineLength xs
maxLineLength :: Int
maxLineLength = 40
所有回复赞赏。谢谢。
对不起。我刚刚意识到这个名字是多么的错误。它应该比较行长度,直到顶行的长度等于底行的长度,然后返回导致其为真的字段宽度。 – ratbum 2011-03-16 20:27:19
@ratbum我已更新我的答案,以解释您添加的信息。 – Rotsor 2011-03-16 20:51:53