简体   繁体   English

Haskell:过滤字符串列表

[英]Haskell : Filtering a list of strings

I have a list of Strings I want to filter through. 我有一个要过滤的字符串列表。 My predicate is that the string should begin with an uppercase letter. 我的判断是该字符串应以大写字母开头。

eg. 例如。 when I run onlyLowercase ["boy", "girl", "Hi"] 当我只运行onlyLowercase ["boy", "girl", "Hi"]

it should give me a list of ["boy", "girl"] 它应该给我一个["boy", "girl"]的清单

I can do it using pattern matching and guards, but I'm using the learnyouahaskell ( http://learnyouahaskell.com ) book and I came across the topic on higher-order functions. 我可以使用模式匹配和防护来做到这一点,但我使用的是Learnyouahaskell( http://learnyouahaskell.com )书,因此我遇到了有关高阶函数的主题。 I read about the filter function and thought it could achieve what I want to do in far fewer lines of code. 我阅读了有关过滤器功能的文章,并认为它可以用更少的代码行实现我想做的事情。

Using pattern Matching/Guards (This works well and solves my problem) 使用模式匹配/防护 (效果很好,可以解决我的问题)

onlyLowercase :: [[Char]] -> [[Char]]
onlyLowercase [] = []
onlyLowercase (x:xs) 
  | isLower (head x) = x : onlyLowercase xs  
  | otherwise = onlyLowercase xs

Using the filter function 使用过滤功能

onlyLowercase2 :: [String] -> [String]
onlyLowercase2 [] = []
onlyLowercase2 (x:xs) = filter isLower x : onlyLowercase2 xs

Unfortunately, when I run onlyLowercase2 ["boy", "girl", "Hi"] , I get a list of ["boy", "girl", "i"] . 不幸的是,当我运行onlyLowercase2 ["boy", "girl", "Hi"] ,得到了["boy", "girl", "i"]

I want to know if there's a way I can filter my list of strings using the first character in my string (without creating any auxiliary function that could check the String and return true if the first letter is lowercase). 我想知道是否有一种方法可以使用字符串中的第一个字符过滤字符串列表(而无需创建任何可以检查String并在第一个字母为小写的情况下返回true的辅助函数)。

I also tried using 我也尝试使用

 onlyLowercase2 (x:xs) = filter (isLower head x) : onlyLowercase2 xs

but that didn't even compile. 但这甚至没有编译。 Basically, I'm just trying to figure out how the filter function can be used on a list of lists. 基本上,我只是想弄清楚如何在列表列表中使用过滤器功能。 Thank you, in advance, for any assistance rendered. 预先感谢您提供的任何帮助。

Thanks to Willem Van Onsem's suggestion to use a lambda expression as a filter function, I read further and came up with this 2 line solution. 感谢Willem Van Onsem建议使用lambda表达式作为过滤器函数,我进一步阅读并提出了这2行解决方案。

onlyLowercase2 :: [String] -> [String]
onlyLowercase2 = filter (\st-> ("" /= st) && (isLower $ head st))

Not sure if it's perfect, but at least it's working. 不知道它是否完美,但至少可以正常工作。

Using Data.List and Data.Char : 使用Data.ListData.Char

import Data.List
import Data.Char

onlyLowerCase :: [String] -> [String]
onlyLowerCase = filter (all isLower)

I use the all function which checks that all elements of a list satisfy a predicate. 我使用all函数检查列表的all元素是否满足谓词。 In this case all isLower will return true if all letters in a String are lowercase. 在这种情况下,如果String中的所有字母均为小写,则all isLower将返回true。 Then just filter the Strings that are all lowercase. 然后只filter所有小写​​的Strings The Haskell Report has a good reference for List and Char functions among other useful libraries. Haskell报告在其他有用的库中为ListChar函数提供了很好的参考。

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

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