'How can I multyply spaces and repair my code in haskell?

Specify the function that appends a text to a given length by appending a sufficient amount of space to the beginning of the text! If the text is not shorter than the required length, it does not need to be modified. The problem is with the a * " ",it is not working. How can I multiply spaces?

format :: (Integral a) => a -> String -> String
format a "b"
  | length "b" <length a = a * " " ++ "b"
  | otherwise = "b" 
Example:
format 5 "" == ""
format 10 "haskell" == "haskell"
format 3 "haskell" == "haskell"


Solution 1:[1]

I dont get your examples, since it just stays the same in every case. That being said, a simple solution for the problem using recursion should be something like this:

format :: (Integral a) => a -> String -> String
format i str = i>length(str) ? (format (i-1) " "+str) : str

Without recursion on the main function you could do this:

format :: (Integral a) => a -> String -> String
format i str = i>length(str) ? (concat $ replicate (i-length(str)) " ")++str : str

This functions both append spaces to the start of the string!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ricardo O.