0

I have list of strings

field :: [String]
field = ["......",
         "......",
         "......",
         "......",
         "......",
         "......"] 

But it is static. I am trying to write function, that take two numbers (length of one string X and number of all strings Y).

createField :: (Int, Int) -> [String]

For example: createField 4, 5

It should return this

["....",
 "....",
 "....",
 "....",
 "...."] 

Can you help me with this?

Drew1515
  • 13
  • 1
  • 2
    The function [`replicate`](https://hoogle.haskell.org/?hoogle=replicate) may be of use to you. Really though, you should make an attempt and ask about any issues that arise with that, rather than just asking for a solution. – AJF Dec 06 '19 at 20:17
  • 1
    first of all, it's `createField (4, 5)`. second, can you write what `createField (4, 1)` should return? or even `createField (4, 0)`? do you see how to get the former from the latter? and then `createField (4, 2)` from `createField (4, 1)`? can this be generalized? – Will Ness Dec 06 '19 at 20:17

1 Answers1

3

Although not wrong, using a 2-tuple for parameters is not very common. It is more Haskell-ish to write a function with signature:

createField :: Int -> Int -> [String]

you can then invoke this with createField 4 5 for example.

You can make make use of replicate :: Int -> a -> [a] here, both to construct the String and the list of Strings. For example:

GHCi> replicate 4 '.'
"...."

I leave it as an exercise to construct the list itself. The function will be of the form:

createField :: Int -> Int -> [String]
createField m n = _ (replicate m '.')

Where the _ is something you need to fill in yourself.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555