Home > Blockchain >  How can I extract each of the first characters of a zip pair of strings in Haskell?
How can I extract each of the first characters of a zip pair of strings in Haskell?

Time:12-07

joinString = zip "aceg" "bdfh"

Becomes this after zipping: [('a', 'b'), ('c', 'd'), ('e', 'f'), ('g','h')]. I want to extract the 'a' , 'c', 'e' and 'g' out from the list of tuples. How can I do that?

I tried fst(unzip joinString) but that gives me the string "aceg" but I want each and every of the characters individually.

CodePudding user response:

As noted in comments, a string is a list of characters, so you have a misunderstanding, but also already the answer.

ghci> ['a', 'b', 'c'] == "abc"
True

CodePudding user response:

Well, if you don't have direct access to the parameters supplied to zip, you can use map from Data.List to apply fst over every element of the zipped list:

ghci> map fst [('a', 'b'), ('c', 'd'), ('e', 'f'), ('g','h')]
"aceg"

As pointed out already, "aceg" == ['a', 'c', 'e', 'g'].

  • Related