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?

1

3 Answers 3

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"
Sign up to request clarification or add additional context in comments.

Comments

2

You can also do this in one handy line

let a = [('a','1'),('b','2'),('c','3')]
uncurry ((. return) . (:)) =<< a

Comments

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.