用haskell匹配2个字符串中的字母

时间:2013-05-22 13:39:27

标签: string haskell match letters

我刚开始使用haskell,我想知道是否有一种简单的方法来匹配2个字符串之间的字母并输出它们。

像:

iced and likes将返回i,e,d

谢谢!

1 个答案:

答案 0 :(得分:5)

使用Data.Set.intersection

 import qualified Data.Set as S

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

编辑:正如@jozefg指出的那样,Data.List中有一个函数对列表执行相同的操作:

 > import Data.List (intersect)
 > intersect "liked" "iced"
 "ied"
相关问题