Pregunta

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!

¿Fue útil?

Solución

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"
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top