我正在尝试将文本格式化为矩形;目前我已经能够让它正确地左对齐,但最后一行没有尽可能地延伸。
我正在尝试计算最佳字段宽度以最小化或完全删除它。
我完全被困住了。下面的代码显示了相关的功能。目前它陷入了无限循环。我哪里错了?
顺便说一句,调试 Haskell 代码的最佳方法是什么?(是的,我对此很陌生。)
bestFieldWidth 应该比较行长,直到顶行的长度等于底行的长度,然后返回使此为真的字段宽度。
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
感谢所有回复。谢谢你。