b30*_*667 6 formatting haskell
我有一个字符串列表列表,我需要使用句点将它们格式化为三角形,使得每个列表都在其自己的行上,并且每个列表的字符串由至少一个句点分隔.此外,每个角色必须在上方和下方有点.这可能是最好的例子解释:
> dots [["test"], ["hello", "world"], ["some", "random", "words"], ["even", "more", "random", "words"]]
Run Code Online (Sandbox Code Playgroud)
应该回来
............test.....................
.......hello.......world.............
...some......random......words.......
not....really......random.....anymore
Run Code Online (Sandbox Code Playgroud)
最后,它应该使用尽可能少的句号,即将每个单词填充到最大单词长度的方法太浪费了; 这是上面的例子不应该返回
.....................test........................
..............hello.........world................
.......some..........random........words.........
not...........really........random........anymore
Run Code Online (Sandbox Code Playgroud)
我可以很容易地编写一个函数来执行将句点放在任何一边使其成为三角形,我的问题是在单词之间的句点.
我有一个函数只要字长为1就可以工作,这对任务来说显然毫无用处.不过,我的功能dots:
dots :: [[String]] -> [[String]]
dots xss = map dots' xss
where dots' (x:[]) = [x]
dots' (x:xs) = [x] ++ ["."] ++ dots' xs
Run Code Online (Sandbox Code Playgroud)
这是一个家庭作业,所以提示是首选,但我一直试图这样做几个小时没有运气.
首先,您需要一个函数,将占位符添加到列表中,如下所示:
addPlaceholders [ ["test"]
, ["hello", "world"]
, ["some", "random", "words"]
, ["not", "really", "random", "anymore"]
]
==> [ ["" , "" , "" , "test" , "" , "" , "" ]
, ["" , "" , "hello" , "" , "world" , "" , "" ]
, ["" , "some", "" , "random", "" , "words", "" ]
, ["not", "" , "really", "" , "random", "" , "anymore"]
]
Run Code Online (Sandbox Code Playgroud)
现在你需要""用点填充这些。因此,您可以编写一个辅助函数,将点添加到列表中:
addDots ["test", "", "random", ""]
==> ["test..","......","random","......"]
Run Code Online (Sandbox Code Playgroud)
然后fill就是简单的
fill = transpose . map addDots . transpose
Run Code Online (Sandbox Code Playgroud)
你的功能只是
triangle = map concat . fill . addPlaceholders
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
135 次 |
| 最近记录: |