简体   繁体   English

在Haskell字符串列表中获取长度为奇数的元素

[英]Get elements with odd length in a Haskell list of strings

I have a list of strings in Haskell and I need to get those elements with odd length in another list. 我在Haskell中有一个字符串列表,我需要在另一个列表中获取具有奇数长度的那些元素。 How can this be done using higher order functions like foldr, foldl, foldr1, foldl1, filter, map, and so on? 如何使用较高阶的函数(例如foldr,foldl,foldr1,foldl1,filter,map等)完成此操作? I will very much appreciate your help. 非常感谢您的帮助。 Can list comprehension be used in this case? 在这种情况下可以使用列表理解吗?

It seems that you are aware that filter exists (since you've mentioned), but perhaps are uncertain how it works. 似乎您已经知道filter存在(因为您已经提到过),但是可能不确定它是如何工作的。 If you're trying to extract a specific subset of a list, this seems to be the right path. 如果您要提取列表的特定子集,这似乎是正确的路径。 If you look at its type-signature, you'll find it's pretty straight-forward: 如果查看其类型签名,您会发现它非常简单:

(a -> Bool) -> [a] -> [a]

That is, it takes a function that returns True or False (ie true to contain in the new set, false otherwise) and produces a new list. 也就是说,它需要一个返回True或False(即包含在新集合中为true,否则为false)并生成新列表的函数。 Similarly, Haskell provides a function called odd in Prelude. 同样,Haskell在Prelude中提供了一个称为odd的功能。 It's signature looks as follows: 它的签名如下所示:

Integral a => a -> Bool

That is, it can take any Integral type and returns True if it is odd, false otherwise. 也就是说,它可以采用任何Integral类型,如果为奇数,则返回True,否则返回false。

Now, let's consider a solution: 现在,让我们考虑一个解决方案:

filter odd [1..10]

This will extract all the odd numbers between [1,10]. 这将提取[1,10]之间的所有奇数。

I noticed you mentioned list comprehensions. 我注意到您提到了列表理解。 You probably do not want to use this if you are already given a list and you are simply filtering it. 如果您已经获得列表,而只是对其进行过滤,则可能不想使用它。 However, a list comprehension would be a perfectly acceptable solution: 但是,列表理解将是一个完全可以接受的解决方案:

[x | x <- [1..10], odd x]

In general, list comprehensions are used to express the generation of lists with more complicated constraints. 通常,列表推导用于表示约束更复杂的列表的生成。

Now, to actually answer your question. 现在,实际回答您的问题。 Since we know we can filter numbers, and if we're using Hoogle searching for the following type (notice that String is simply [Char] ): 既然我们知道我们可以过滤数字,并且如果我们使用Hoogle搜索以下类型(请注意String就是[Char] ):

[a] -> Int

You will see a length function. 您将看到一个length函数。 With some function composition, we can quickly see how to create a function which filters odd length. 通过一些函数组合,我们可以快速了解如何创建一个过滤奇数长度的函数。 In summary, we have odd which is type Int -> Bool (in this case) and we have length which is [a] -> Int or-- specifically-- String -> Int . 总而言之,我们有一个odd ,其类型为Int- Int -> Bool (在这种情况下), length[a] -> Int或-特别是-String- String -> Int Our solution now looks like this: 现在,我们的解决方案如下所示:

filter (odd . length) ["abc","def","eh","123","hm","even"]

Here ya go. 你去。

getOddOnes = filter . flip (foldr (const (. not)) id) $ False

Note: if you turn this in for your homework, you'd best be prepared to explain it! 注意:如果您上交作业,则最好做好解释!

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

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