I'l looking for an idiomatic (perhaps built-in) way of padding string representations of integers with zeros on the left.
In my case the integers are never more than 99 so
fix r = if length r == 1 then '0':r else r
fix.show <$> [1..15]
works. But I expect there is a better way.
How do I pad string representations of integers in Haskell?
For the sake of completeness, I add here a program padding any list of strings with a character passed as an argument.
Rather than taking the maximum of the lengths from the get go, I use an instance of circular programming: if you look carefully, you'll see that
n
is the result of a computation... in which it is used!printf
style formatting is availble via theText.Printf
module:Or to special case the formatting of 0:
The above exploits the fact that
replicate
returns the empty string on negative argument.