Reputation: 33
I've a field made of list of lists like this:
let list1 = ["a", "abcdefgh", "abc"]
I want to get a new field 5x5 such that there could be more then 5 lines or columns, but no less. It must be produced from the previous one by addition spaces ' ' or keeping them as they are, if there are more then 5 symbols. For additional lines no spaces needed. For example:
list2 = ["a ", "abcdefgh", "abc ", " ", " "]
or:
let list1 = ["a", "abcdefgh", "c", "d", "e", "f"]
list2 = ["a ", "abcdefgh", "c ", "d ", "e ", "f"]
I thought about map, replicate, and length, but I'm a little bit confused. If it's possible, don't use libs (or use Data.Map only). Thank you
Upvotes: 0
Views: 402
Reputation: 477533
You need to determine the length of the strings. You can do this for example with a lambda expression, so then the mapping looks like:
func :: [String] -> [String]
func mylines = map (\x -> … ) linesOfFiles ++ replicate (5-length mylines) (replicate 5' ')
where x
is thus string that will be mapped, and …
is an expression to what that value is mapped. I leave filling in …
as an exercise.
Usually it is better not to work with length
: length
takes linear time, and for infinite lists, this will even get the program into an infinite loop.
You can work with recursion to perform padding, something like:
padding :: Int -> a -> [a] -> [a]
padding 0 _ xs = …
padding n x [] = …
padding n x (x:xs) = …
Upvotes: 1