简体   繁体   English

haskell:过滤具有特定符号的字符串,例如'!' 列表中?

[英]haskell: filtering Strings with a certain symbol e.g '!' in a List?

I have a list of Strings : ["1*1", "ab!c", "cde2", "efghi!"] I want to sort out every string with an '!' 我有一个字符串列表: ["1*1", "ab!c", "cde2", "efghi!"]我想用'!'来整理每个字符串 . My first idea was : filter (map elem '!' (list)) list String with map elem '!' 我的第一个想法是: filter (map elem '!' (list)) list string with map elem'!' (list), but thats not really working because it only checkups the String as itself and not the elements of the Strings in that List... (列表),但那不是真的有效,因为它只检查字符串本身而不是该列表中字符串的元素...

thanks for your help! 谢谢你的帮助!

You mapped too much. 你映射得太多了。

filter ('!' `elem`) list

should work for you.... 应该为你工作....

Explanation: 说明:

You have a list of strings [String] . 你有一个字符串列表[String] A string is a list of chars, because String = [Char] ... 字符串是字符列表,因为String = [Char] ...

So basically you have a list of lists. 所以基本上你有一个列表列表。 [[Char]] Since elem works by checking if a single element exists in a list, you can just call elem on each list item from your outer list. [[Char]]由于elem通过检查列表中是否存在单个元素来工作,因此您只需在外部列表中的每个列表项上调用elem即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM