I want a function that takes two lists of any type and returns one (i.e. f:: [[a]] -> [[a]] -> [[a]]
). Basically, too produce the 'concatenation' of the two input lists.
e.g.
> f [[1,2,3], [123]] [[4,5,6], [3,7]]
[[1,2,3,4,5,6], [1,2,3,3,7], [123,4,5,6], [123,3,7]]
I currently have got this far with it:
f _ [] = []
f [] _ = []
f (xs:xss) (ys:yss) = ((xs ++ ys) : [m | m <- f [xs] yss])
But this doesn't take into account xss
and is wrong. Any suggestions?
To make a list [1,2,3] you just build it up with 1 : 2 : 3 : [] . The first element of : is the item to add on the front of the list, and the second element is either a list (also built up with : or the empty list signified by [] ). The ++ operator is list concatenation. It takes two lists and appends them together.
What does concat do in Haskell? concat flattens a list of lists into just a list of elements.
The CONCATENATE function will give the result as a text string. The function converts numbers to text when they are joined.
It's a Cartesian product, so you can simply use one list comprehension to do everything.
Prelude> let xs = [[1,2,3], [123]]
Prelude> let ys = [[4,5,6], [3,7]]
Prelude> [x ++ y | x <- xs, y <- ys]
[[1,2,3,4,5,6],[1,2,3,3,7],[123,4,5,6],[123,3,7]]
import Control.Applicative
(++) <$> [[1,2,3], [123]] <*> [[4,5,6], [3,7]]
[[1,2,3,4,5,6],[1,2,3,3,7],[123,4,5,6],[123,3,7]]
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