[英]What is the type of foldr map in haskell?
I am trying to find out what the type of foldr map is, and how you should be solving something like this. 我试图找出折叠地图的类型,以及你应该如何解决这样的问题。
I know what the individual types are: 我知道个别类型是什么:
foldr :: (a -> b -> b) -> b -> [a] -> b
map :: (a -> b) -> [a] -> [b]
I know how the individual functions work, but finding out the type is something I just can't seem to solve. 我知道各个函数是如何工作的,但找出类型是我似乎无法解决的问题。
foldr would take a function as first parameter, which would be the whole of map right? foldr会将一个函数作为第一个参数,这将是整个映射的权利吗?
All tips are welcome, I am new to Haskell and trying to learn puzzles like these. 我欢迎所有提示,我是Haskell的新手,并试图学习这些谜题。
As ingredients we have foldr
and map
. 作为成分,我们有foldr
和map
。 To avoid confusion, let us rename the a
and b
of map
to c
and d
, since those are (possibly) different types. 为避免混淆,让我们将map
的a
和b
重命名为c
和d
,因为这些(可能)是不同的类型。 So we take as functions: 所以我们把功能看作:
foldr :: (a -> b -> b) -> b -> [a] -> b
map :: (c -> d) -> [c] -> [d]
or more verbose: 或者更详细:
foldr :: (a -> (b -> b)) -> (b -> ([a] -> b))
map :: (c -> d) -> ([c] -> [d])
Since map
is the parameter of a function application with foldr
as function, this means that the type of map
should be the same as the type of the parameter of foldr
, hence: 由于map
是具有foldr
作为函数的函数应用程序的参数,这意味着map
的类型应该与foldr
的参数类型相同,因此:
a -> (b -> b)
~ (c -> d) -> ([c] -> [d])
----------------------------------
a ~ (c -> d), b ~ [c] ~ [d], c ~ d
So we have derived that a
is the same type as c -> d
, and that b
is the same type as [c]
and [d]
. 因此我们推导出a
与c -> d
类型相同,并且b
与[c]
和[d]
类型相同。 Therefore we also know that c ~ d
( c
is the same type as d
). 因此我们也知道c ~ d
( c
与d
类型相同)。
The type of foldr map
is the return type of the foldr
function, but specialized with the equality relations we have derived, so: foldr map
的类型是foldr
函数的返回类型,但是专门用于我们派生的相等关系,因此:
foldr map :: b -> ([a] -> b)
so we replace a
with c -> c
, and b
with [c]
, hence the type: 所以我们用c -> c
替换a
,用[c]
替换b
,因此类型:
foldr map :: [c] -> ([c -> c] -> [c])
or in a less verbose form: 或者是一种不那么冗长的形式:
foldr map :: [c] -> [c -> c] -> [c]
Note : the signature of
foldr
has been generalized tofoldr :: Foldable f => (a -> b -> b) -> b -> fa -> b
, but deriving the type is similar. 注意 :foldr
的签名已经推广到foldr :: Foldable f => (a -> b -> b) -> b -> fa -> b
,但派生类型是类似的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.