Difference between lifting and higher order functions

孤人 提交于 2019-12-06 08:25:09

I'm surprised no one has answered this already.

A lifting function's role is to lift a function into a context (typically a Functor or Monad). So lifting a function of type a -> b into a List context would result in a function of type List[a] -> List[b]. If you think about it this is exactly what map (or fmap in Haskell) does. In fact, it is part of the definition of a Functor.

However, a Functor can only lift functions of one argument. We also want to be able to deal with functions of other arities as well. For example if we have a function of type a -> b -> c we cannot use map. This is where a more general lifting operation comes into the picture. In Haskell we have a lift2 for this case:

lift2:: (a -> b -> c) -> (M[a] -> M[b] -> M[c])

where M[a] is some particular Monad (like List) parameterized with a given type a.

There are additional variants of lift defined as well for other arities.

This is also why filter is not a lifting function as it doesn't fit the type signature required; you are not lifting a function of type a -> bool to M[a] -> M[bool]. It is, however, a higher-ordered function.

If you want to read more about lifting the Haskell Wiki has a good article on it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!