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?
Noteworthy: Haskell already has that function, just a bit more general:
Prelude> :t sequence
sequence :: Monad m => [m a] -> m [a]
Prelude> sequence ["ab","cd","12"]
["ac1","ac2","ad1","ad2","bc1","bc2","bd1","bd2"]
[]
is an instance of Monad
, so in this case the signature becomes sequence :: [[a]] -> [[a]]
, with a = Char
, sequence :: [String] -> [String]
.
Try
combineStrings [] = [""]
or better (as pointed out by sdcwc):
combineStrings [] = [[]]
Otherwise the part b <- combineStrings tl
of the list comprehension will not yield any b
and 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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With