Last active
December 10, 2015 10:28
-
-
Save folone/4421009 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hs [master●] % ghci ~/workspace/folone.github.com/docs/hs | |
GHCi, version 7.6.1: http://www.haskell.org/ghc/ :? for help | |
Loading package ghc-prim ... linking ... done. | |
Loading package integer-gmp ... linking ... done. | |
Loading package base ... linking ... done. | |
Prelude> :l NewYear | |
[1 of 1] Compiling Main ( NewYear.hs, interpreted ) | |
Ok, modules loaded: Main. | |
*Main> :t congratulation | |
congratulation :: Happy NewYear | |
*Main> congratulation | |
Happy (NewYear 2013) | |
*Main> bonusCongratulation | |
("Happy New Year",2013) | |
*Main> :q | |
Leaving GHCi. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Applicative | |
data NewYear = NewYear Int | |
deriving (Eq, Show) | |
data Happy a = Happy a | |
deriving (Eq, Show) | |
instance Functor Happy where | |
fmap f (Happy a) = Happy $ f a | |
instance Applicative Happy where | |
pure = Happy | |
(Happy f) <*> (Happy x) = Happy (f x) | |
from :: Int -> Happy Int | |
from = pure . (+ 1) | |
congratulation :: Happy NewYear | |
congratulation = Happy NewYear <*> from 2012 | |
-- http://stackoverflow.com/questions/10937268/applicative-instance-for-a-tuple-with-monoid-and-function-inside | |
bonusCongratulation = ("Happy ", (*)) <*> ("New ", 671) <*> ("Year", 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment