Вопрос

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!

Это было полезно?

Решение

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"
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top