本文介绍了在Haskell中将功能混淆为Functor的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在Functor中的fmap类型是: fmap :: Functor f => (a - > b) - > f a - > fb 看起来像,首先将函数(a - > b)应用于fa的参数以创建b类型的结果,然后将f应用于它,结果是fb 使用Maybe a例如:b fmap show(Just 1)结果是:只是1 同样说: Just(显示1) 但当( - >)用作Functor时(在Control.Monad.Instances中) import Control.Monad.Instances (fmap show Just)1 结果是:只有1 即,先应用,然后显示应用。在另一个例子中,结果是一样的: fmap(* 3)(+100)1 结果是303 为什么不是* 3先,然后是+100?$ / b> Functor中的fmap类型为: fmap :: Functor f => (a - > b) - > f a - > fb 看起来,首先将函数(a - > b)应用于fa $ b的参数$ b来创建类型b的结果,然后将f应用于它,结果是fb 这是 fmap ,但是你对这种类型意味着什么是错误的解释。 你似乎认为 fa 有一个参数,并且该参数的类型为 a 。 考虑 $ li $也许 xs = 也许 xs = [x1] 。 也许 xs = [x1,x2] 。 ... $ b 类型 fa 是一个函子 f 使用单个类型参数 a 。但 不一定采用 F x 的形式,因为您可以现在考虑 fmap f xs : $ b $ b 也许 fmap f xs = [] 。 c $ c $> fmap f xs = [f x1] 。 也许 fmap f xs = [f x1,f x2] 。 ... (第一种情况)! f !或者我们可能会多次应用它(第三种情况)。 我们所做的是替换 a ,类型为 b 的东西。但是,我们保留了较大的结构 - 没有添加任何新元素,也没有删除元素,他们的订单保持不变。 现在让我们考虑函子(c - >)。 (请记住,一个函子只接受一个类型参数,所以( - >)的输入是固定的。) 是否有一个 c - >一个甚至包含 a ?它可能根本不包含任何 a s,但当我们给它一个 c 。但是 fmap 的结果是类型为 c - > b :我们只需要提供一个 b ,当我们看到 c 所以我们可以说 fmap fx = \y - > f(xy)。 在这种情况下,我们将 f 应用于需求---每当我们返回的函数被应用时,也应用 f 。 the type of fmap in Functor is:fmap :: Functor f => (a -> b) -> f a -> f bit looks like ,first apply function (a -> b) to the parameter of f a to create a result of type b, then apply f to it, and result is f busing Maybe a for example : fmap show (Just 1) result is : Just "1"same as saying:Just (show 1)but when (->) is used as a Functor (in Control.Monad.Instances)import Control.Monad.Instances(fmap show Just) 1result is : "Just 1"that is, Just is apply first, then show is applied. in another example ,result is same: fmap (*3) (+100) 1 result is 303why not *3 first, then +100? 解决方案 That is the type of fmap, but your interpretation of what that type means is wrong.You seem to assume that f a has one parameter, and that that parameter has type a.Consider xs :: [a]:Perhaps xs = [].Perhaps xs = [x1].Perhaps xs = [x1, x2]....The type f a is a functor f with a single type parameter a. But values of type f a do not necessarily take the form F x, as you can see from the first and third cases above.Now consider fmap f xs:Perhaps fmap f xs = [].Perhaps fmap f xs = [f x1].Perhaps fmap f xs = [f x1, f x2]....We don't necessarily apply f at all (first case)! Or we might apply it more than once (third case).What we do is replace the things of type a, with things of type b. But we leave the larger structure intact --- no new elements added, no elements removed, their order is left unchanged.Now let's think about the functor (c ->). (Remember, a functor takes one type parameter only, so the input to (->) is fixed.)Does a c -> a even contain an a? It might not contain any as at all, but it can somehow magic one out of thin air when we give it a c. But the result from fmap has type c -> b: we only have to provide a b out of that when we're presented with a c.So we can say fmap f x = \y -> f (x y).In this case, we're applying f on demand --- every time the function we return gets applied, f gets applied as well. 这篇关于在Haskell中将功能混淆为Functor的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 09:01