-1

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!

Sevo
  • 13
  • 1
  • 5
  • 1
    Please specify more carefully what you mean with "match". Does the order of the letters count? What if there are multiples? From the information you've provided so far, this can be anything from a simple set intersection to a [longest common subsequence](https://en.wikipedia.org/wiki/Longest_common_subsequence) problem. – hammar May 22 '13 at 13:47
  • I mean the matching letter within the strings just like the example i gave. If you put in bold and cold the return string will be o,l,d because both strings use those 3 letters – Sevo May 22 '13 at 13:49
  • 1
    What about `dloc` and `bold`? Or `boooold` and `coold`? – hammar May 22 '13 at 13:50
  • @Sevo You're happy with the answer, but please could you answer hammar's questions, and edit your question with that information? – AndrewC May 24 '13 at 12:41

1 Answers1

5

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"
Dmytro Sirenko
  • 5,003
  • 21
  • 26
  • Perhaps it's worth mentioning the list equivalent since the oP is new to Haskell and probably doesn't knwo about sets – daniel gratzer May 22 '13 at 13:51
  • haha im trying to let it work, only got error outputting this: import Data.List (intersect) main = do putStrLn "Woord1:"; a <- readLn putStrLn "Woord2:"; b <- readLn putStrLn intersect((read a) (read b)) – Sevo May 22 '13 at 14:02
  • hmm maybe hard to read the code like that way? – Sevo May 22 '13 at 14:02
  • 1
    @Sevo: Try [something like this](http://hpaste.org/88351). – hammar May 22 '13 at 14:13