I do string mingling:
main = do
first <- getLine
second <- getLine
print (zip first second)
result: [('a','1'),('b','2'),('c','3')]
.
But how I can concate result to a1b2c3
?
I do string mingling:
main = do
first <- getLine
second <- getLine
print (zip first second)
result: [('a','1'),('b','2'),('c','3')]
.
But how I can concate result to a1b2c3
?
You have to do the following operations:
1) Convert the tuples inside the list to a single String which can be performed by a single map
.
2) Concatenate the entire list
Prelude> let a = [('a','1'),('b','2'),('c','3')]
Prelude> let b = map (\(x,y) -> x : [y]) a
Prelude> b
["a1","b2","c3"]
Prelude> concat b
"a1b2c3"
Prelude> Prelude.foldl1' (++) b -- instead of concat you can do fold
"a1b2c3"
You can also do this in one handy line
let a = [('a','1'),('b','2'),('c','3')]
uncurry ((. return) . (:)) =<< a
If I may add yet another (unnecessary) answer, just to show in how many (crazy) ways Haskell lets you think and solve a problem :)
Prelude Data.List> let input = [('a','1'),('b','2'),('c','3')]
Prelude Data.List> let (l1,l2) = unzip input
Prelude Data.List> let alt_stream s1 s2 = head s1:head s2:alt_stream (tail s1) (tail s2)
Prelude Data.List> take (length l1 * 2) $ alt_stream (cycle l1) (cycle l2)
"a1b2c3"