我可以理解问题是否能真正解决我的问题,所以这里有更多解释:
我正在尝试在字符串的开头添加字符串“ +”,如下所示:
printLine :: [Int] -> String --Type of the function
printLine [] = "" --Base case
printLine (x:xs) = "+" ++ foldr (++) "+" f ++ printLine xs
where f = replicate x "-"
我从上面得到的结果:
+-----++------++------++------+
我想要得到的结果:
+-----+------+------+------+
基本上我的问题是:如何仅在开头添加“ +”? 我可以理解这可能是一个愚蠢的问题,但是我被困了一段时间,无法在SO或其他地方找到答案。
答案 0 :(得分:9)
建议:不要检测您何时处于第一次迭代中,这很困难;而是检测您何时处于上一次迭代中,这很容易,因为在第一行中就是[]
情况。
printLine :: [Int] -> String
-- final iteration; add an extra + at the end
printLine [] = "+"
-- not the final iteration; don't include a + at the end of the -s
printLine (x:xs) = "+" ++ replicate x '-' ++ printLine xs
答案 1 :(得分:0)
如果一个空列表必须映射到一个空字符串,则一种选择是用特殊情况折叠一个空列表。
printLine :: [Int] -> String
printLine [] = ""
printLine xs = foldr (\x res -> '+' : replicate x '-' ++ res) "+" xs
如此
λ> map printLine [[], [1..4], [5]]
["","+-+--+---+----+","+-----+"]
或者,由于最初的问题在第一次迭代中要求控制,因此一个选择是使用辅助函数。这是两种选择。
printLine' :: [Int] -> String
printLine' [] = ""
printLine' xs = '+' : go xs
where go :: [Int] -> String
go [] = ""
go (n:ns) = replicate n '-' ++ "+" ++ go ns
printLine'' :: [Int] -> String
printLine'' xs = go True xs
where go :: Bool -> [Int] -> String
go _ [] = ""
go isFirst (n:ns) = (if isFirst then "+" else "")
++ replicate n '-' ++ "+" ++ go False ns
具有这些定义
λ> map printLine' [[], [1..4], [5]]
["","+-+--+---+----+","+-----+"]
λ> map printLine'' [[], [1..4], [5]]
["","+-+--+---+----+","+-----+"]