Is there a standard function that computes `fx (gx)`?

I could not find anything in Hoogle, but is there a standard function or operator with a signature like:

func :: (a -> b -> c) -> (a -> b) -> a -> c

those. two functions are given fand gand the element xas arguments, which it calculates f x (g x)?

+4
source share
3 answers

f <*> g ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

See https://wiki.haskell.org/Pointfree .

+9
source

The function you are looking for is (<*>). What for? Well, it's true that it (<*>)has a more general type:

(<*>) :: Applicative f => f (a -> b) -> f a -> f b

But keep in mind that we can specialize fup to (->) rwhich has an instance Applicative:

(<*>) :: (->) r (a -> b) -> (->) r a -> (->) r b

..., , -> infix , :

(<*>) :: (r -> a -> b) -> (r -> a) -> (r -> b)

..., -.

, (->) Functor, Applicative Monad, "". , .

+10

, ap :: Monad m => m (a -> b) -> m a -> m b

m (->) r, . ap []:

ap m1 m2 = do
    x1 <- m1
    x2 <- m2
    return (x1 x2)

, , :

ap m1 m2 = m1 >>= (\x1 -> m2 >>= return . x1)

>>= (->) r []:

instance Monad ((->) r) where
    f >>= k = \ r -> k (f r) r
    return = const

(return pure, const).

, , :

ap f g = f >>= (\x1 -> g >>= const . x1)
       = f >>= (\x1 -> (\r -> (const . x1) (g r) r))
       = \x -> (\x1 -> (\r -> (const . x1) (g r) r)) (f x) x

- (x1 is (f x)):

ap f g = \x -> (\r -> (const . (f x)) (g r) r) x

- (r - x):

ap f g = \x -> (const . (f x)) (g x) x

const \c _ -> c (.) f . g `\ z → f (g z):

ap f g = \x -> ((\c _ -> c) . (f x)) (g x) x
       = \x -> (\z -> (\c _ -> c) ((f x) z)) (g x) x

- (z is (g x), c - ((f x) (g x))):

ap f g = \x -> ((\c _ -> c) ((f x) (g x))) x
       = \x -> (\_ -> ((f x) (g x))) x

, - (_ is x):

ap f g = \x -> ((f x) (g x))

x :

ap f g x = (f x) (g x)

Haskell f x y (f x) y, , :

ap f g x = (f x) (g x)
         = f x (g x)

.

+2

Source: https://habr.com/ru/post/1690633/


All Articles