部分应用函数类型的函子 [英] functors from partially applied function type
本文介绍了部分应用函数类型的函子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Haskell编程中有一个问题说:完成以下声明:
there is a quesion in Programming in Haskell that says: Complete the following declaration:
instance Functor ((->) a) where
现在作为Functor Thing的类型定义为:
Now as Functor Thing has a type definition of:
instance Functor Thing where
--fmap::(a -> b) -> Thing a -> Thing b
我想知道这种减少是否有意义:
I was wondering if this reduction makes sense:
instance Functor ((->) a) where
-- fmap::(a -> b) -> ((->) a) a -> ((->) a) b
-- therefore
-- fmap::(a -> b) -> a -> a -> (a -> b)
-- therefore
-- fmap::b -> b
-更新--- 我错过了括号,应该是
-- update --- I missed brackets, it should have been
instance Functor ((->) a) where
-- fmap::(a -> b) -> ((->) a) a -> ((->) a) b
-- therefore
-- fmap::(a -> b) -> (a -> a) -> (a -> b)
-- therefore
-- I should be returning a function of a -> b
推荐答案
否,因为实例声明中的 a
与中的 a
不同 fmap
的类型.您需要在实例声明中分配一个类型变量,以避免在捕获" fmap
的类型:
No, because the a
in your instance declaration is not the same a
as the one in the type of fmap
. You need to assign a type variable in your instance declaration that avoids "capturing" the a
in the type of fmap
:
instance Functor ((->) r) where
fmap :: (a -> b) -> (r -> a) -> (r -> b)
这篇关于部分应用函数类型的函子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文