pip
pip

Reputation: 75

Padding an empty string to text

I am do a crypto exercise that I need to pad an input text to have length of multiple of 16 bytes (AES), and I find that in python I can create a empty (i.e. string of space) with:

' ' * n # whatever integer n is

Is there an equivalent way in haskell? I can do it with simple function using recursion, but just curious is there a way that is even shorter than the python snip.

Upvotes: 0

Views: 584

Answers (1)

Jon Purdy
Jon Purdy

Reputation: 55069

Since strings are lists of characters, you can use:

replicate :: Int -> a -> [a]

For example:

replicate 5 'x' == "xxxxx"

You can find utility functions like this yourself by searching for a plausible type signature with Hoogle; replicate happens to be the first result in a Hoogle search for Int -> a -> [a].

If you’re using Text instead of String, there is an equivalent function in Data.Text:

replicate :: Int -> Text -> Text

replicate 5 (pack "x") == pack "xxxxx"

Upvotes: 2

Related Questions