If I have two strings I use a list comprehension to obtain the desired result:
combineStrings firstStr sndStr = [ [a,b] | a <- firstStr, b <- sndStr]
For three strings, I use this
combineStrings firstStr sndStr trdStr = [ [a,b,c] | a <- firstStr, b <- sndStr, c <- trdStr]
What I’m trying is to obtain the same result for a variable number of strings. For example if I have a function which takes the following form:
combineStrings :: [String] -> [String]
I’m trying to obtain the same results as above for 2, 3 … n lists… I tried multiple ways, like this one
combineStrings [] = []
combineStrings (hd:tl) = [ a:b | a <- hd, b <- combineStrings tl]
but this fails because of [] on the first clause. Can someone help me to write this, please?
Try
or better (as pointed out by sdcwc):
Otherwise the part
b <- combineStrings tlof the list comprehension will not yield anyband you will always end up with an empty array.It also makes sense as an edge case: The only way to combine characters from zero strings is an empty string (consisting of zero characters).