0

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?

rel1x
  • 2,351
  • 4
  • 34
  • 62
  • 2
    See this question: [Interleave List of Lists in Haskell](http://stackoverflow.com/questions/14186433/interleave-list-of-lists-in-haskell) – Rufflewind Dec 10 '14 at 08:18

3 Answers3

3

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"
Sibi
  • 47,472
  • 16
  • 95
  • 163
2

You can also do this in one handy line

let a = [('a','1'),('b','2'),('c','3')]
uncurry ((. return) . (:)) =<< a
Arnon
  • 2,237
  • 15
  • 23
1

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"
bmk
  • 1,548
  • 9
  • 17