为什么要设置"map(过滤器fst)"?类型"[[[(Bool,a)]]->[[(Bool,a)]]"? [英] Why has "map (filter fst)" the type "[[(Bool, a)]] -> [[(Bool, a)]]"?

查看:59
本文介绍了为什么要设置"map(过滤器fst)"?类型"[[[(Bool,a)]]->[[(Bool,a)]]"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图了解该功能的作用

I'm trying to understand why the function

map (filter fst)

具有类型

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

如果过滤器必须接收一个返回Bool-Type的函数,而fst仅返回一个元组的第一个元素,那么过滤器fst"如何工作?

How can "filter fst" work if filter must receive a function which returns a Bool-Type and fst just returns the first element of a tuple?

filter :: (a -> Bool) -> [a] -> [a]
fst :: (a, b) -> a

有人可以解释我吗?谢谢;)

Can anyone explains me? Thanks ;)

推荐答案

如果filter必须接收一个返回Bool-Type并且fst仅返回一个元组的第一个元素的函数,那么"filter fst"如何工作?

How can "filter fst" work if filter must receive a function which returns a Bool-Type and fst just returns the first element of a tuple?

从某种意义上说,您已经回答了自己的问题!让我们分解一下:

In a sense, you've answered your own question! Let's break it down:

过滤器必须接收一个返回布尔类型的函数

filter must receive a function which returns a Bool-Type

好的,让我们看看您要传递的内容: fst . fst 是一个函数吗?是的,是的,所以我们已经将第一部分讲完了.它是否返回 Bool ?好吧,让我们看看它的作用:

OK, so let's look at what you're passing it: fst. Is fst a function? Yes, it is, so we've got the first part down. Does it return a Bool? Well, let's look at what it does:

fst仅返回元组的第一个元素

fst just returns the first element of a tuple

因此,如果元组的第一个元素是 Bool ,那么是的,它确实会返回bool!但是,如果元组的第一个元素不是 Bool 之外的任何其他元素,则不会,并且将使类型检查失败.

So if the first element of a tuple is a Bool, then yes, it does return a bool! If the first element of a tuple is anything other than a Bool, though, it doesn't and will fail the typecheck.

让我们再来看一下您设置的类型.我将更改类型变量的名称只是为了使事情更清楚:

Let's have another look at the types you put up. I'm going to change the names of the type variables just to make things clearer:

filter :: (a -> Bool) -> [a] -> [a]
fst :: (b, c) -> b

fst 接受一个(b,c)并返回一个 b ,并且过滤器期望一个接受 a的函数并返回 Bool .我们传入的是 fst ,因此上面的 a 必须为(b,c),因为这是 fst .我们传递给 filter 的函数的返回值必须是 Bool ,因此上面的 b 必须是 Bool .而且 c 可以是任何东西,因为过滤器根本不使用它.将 a b 的值替换为 filter fst 的最终类型为:

fst takes an (b, c) and returns an b, and filter is expecting a function which takes an a and returns a Bool. We are passing in fst, so the a above must be (b, c) as that's the first parameter of fst. The return value of the function we pass into filter must be a Bool, so b above must therefore be a Bool. And c can be anything, because it's not used by filter at all. Substituting the values for a and b gives us a final type for filter fst of:

filter fst :: [(Bool, c)] -> [(Bool, c)]

最后,地图的类型为:

map :: (d -> e) -> [d] -> [e]

(同样,我在这里重命名了类型变量,只是为了将它们与上面使用的变量区分开来,但是请记住,只要它们在类型注释的范围)

(Again, I've renamed the type variables here, just to differentiate them from the ones we've used above, but remember it doesn't actually matter what they're called so long as they're consistent within the scope of the type annotation)

map(过滤器fst)将上面定义的 filter fst 传递给 map 的第一个参数.将参数替换为 d ,并将结果替换为 e ,我们可以看到此函数必须为 [(Bool,c)]->.[(Bool,c)] ,换句话说, d e 都是(Bool,c).将这些插入功能中,我们得到最终类型:

map (filter fst) passes the filter fst we defined above as the first parameter to map. Substituting the parameter for d and the result for e we can see this function must be [(Bool, c)] -> [(Bool, c)], in other words both d and e are (Bool, c). Plugging those into the function we arrive at the final type:

map (filter fst) :: [[(Bool, c)]] -> [[(Bool, c)]]

这篇关于为什么要设置"map(过滤器fst)"?类型"[[[(Bool,a)]]->[[(Bool,a)]]"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆