Skip to content

Commit 493ddfb

Browse files
committed
Add third example
1 parent 700e821 commit 493ddfb

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Haskell/maybe3.hs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Prelude hiding (Applicative(..))
2+
3+
class Functor f => Applicative f where
4+
pure :: a -> f a
5+
(<*>) :: f (a -> b) -> f a -> f b
6+
(*>) :: f a -> f b -> f b
7+
(<*) :: f a -> f b -> f a
8+
9+
a *> b = fmap const b <*> a
10+
(<*) = flip (*>)
11+
{-# MINIMAL pure, (<*>) #-}
12+
13+
instance Applicative Maybe where
14+
pure = Just
15+
(Just f) <*> v = fmap f v
16+
Nothing <*> _ = Nothing
17+
18+
main = do
19+
let a = Just 5
20+
b = Just $ (* 2) . (+ 5) . (^ 2)
21+
c = b <*> a
22+
print c
23+
24+

0 commit comments

Comments
 (0)