Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fmap fork functions

Tags:

haskell

I want to compose functions in the following way:

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

So that we can use it in the following way:

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

I think it could be simplified with something like 'fmap', so that it needn't define 'compose' at all. But I failed to figure out how to do that.

like image 734
Larry LIU Xinyu Avatar asked Aug 24 '11 09:08

Larry LIU Xinyu


1 Answers

If you import Control.Applicative, then

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

So, you can write (==) <$> (myReverse . myReverse) <*> id $ [1..100]

<*> specialized to functions is equivalent to the S-combinator:

s f g x = f x (g x)

You can probably use Control.Arrow too:

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

Update

I've asked lambdabot at #haskell the same question and he answered simply liftM2. :D

like image 165
Rotsor Avatar answered Sep 20 '22 23:09

Rotsor