如何在 Haskell 中递归的字符串后添加 n 个空格?

How can I add n whitespaces after a string with recursion in Haskell?

例如给定一个数字(5)和一个字符串(apple),程序掉线(apple_____)。 我想用递归来做到这一点。我这样试过:

add :: Integer -> [Char] -> [Char]
add 0 (x:xs) = (x:xs)
add i [] = add (i-1) [] ++ " "
add i (x:xs) = format (i-1) xs

鉴于你想添加0 spaces,你可以return给定的字符串(1);如果字符串是 non-empty,则发出字符串的第一个元素 x 并在字符串的尾部递归 (2);最后,如果我们到达字符串的末尾,并且 i 大于 0,我们将发出一个 space,并递归 i 比给定的 [=13] 小一个=] (3):

add :: Integer -> String -> String
add i xs | i <= 0 = xs  -- (1)
add i (x:xs) = x : …    -- (2)
add i [] = ' ' : …      -- (3)

你这里还需要填写部分。