[Haskell-beginners] Functions as Applicatives

Imants Cekusins imantc at gmail.com
Mon Aug 22 17:34:11 UTC 2016


Hello Ollumide,

this may help: it builds and runs anyway.


‚Äč{-
instance Applicative ((->) r) where
    pure x = (\_ -> x)
    f <*> g = \x -> f x (g x)

with the following expression

ghci> :t (+) <$> (+3) <*> (*100)
(+) <$> (+3) <*> (*100) :: (Num a) => a -> a
ghci> (+) <$> (+3) <*> (*100) $ 5
508

-}

f::Num f => f -> f -> f
f = (+)

g::Num g => g -> g
g = (+ 3)

h::Num h => h -> h
h = (* 100)


fg::Num a =>     a -> a -> a
fg = f <$> g
{-  fg a b = a + (b + 3)
    fg a = \b -> a + (b + 3)
-}

fgh::Num a =>  a -> a
fgh = fg <*> h
{-  fgh b = fg (b * 100)
    fgh = \b -> fg (b * 100)
-}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160822/6ea7ca9b/attachment-0001.html>


More information about the Beginners mailing list