fmap fork函数

时间:2011-08-24 09:18:32

标签: haskell

我想以下列方式编写函数:

compose :: (a->b->c) -> (d->a) -> (d->b) -> d -> c
compose f g h x = f (g x) (h x)

这样我们就可以通过以下方式使用它:

compose (==) (myReverse . myReverse) id [1..100]

我认为它可以用'fmap'之类的东西进行简化,因此根本不需要定义'compose'。但我没弄明白该怎么做。

1 个答案:

答案 0 :(得分:11)

如果您导入Control.Applicative,那么

compose f g h = f <$> g <*> h

所以,你可以写(==) <$> (myReverse . myReverse) <*> id $ [1..100]

专门针对函数的

<*>等同于S-combinator

s f g x = f x (g x)

您也可以使用Control.Arrow

compose f g h = g &&& h >>> uncurry f
test = uncurry (==) <<< (myReverse <<< myReverse) &&& id $ [1..100]

更新

我在lambdabot询问#haskell同样的问题,他只回答liftM2。 :d