Question

I just started with haskell and im wondering if there is a easy way to match the letters between 2 string and output them.

like:

iced and liked will return i,e,d

Thank you!

Was it helpful?

Solution

Use Data.Set.intersection:

 import qualified Data.Set as S

 sharedLetters str1 str2 = S.toList $ S.intersection (S.fromList str1) (S.fromList str2)

EDIT: As @jozefg pointed out, there is a function in Data.List which does the same for lists:

 > import Data.List (intersect)
 > intersect "liked" "iced"
 "ied"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top