Skip to content

Instantly share code, notes, and snippets.

@chiller
Created June 2, 2016 17:26
Show Gist options
  • Save chiller/377aa5944d620cedb98bcc0ff0a8056b to your computer and use it in GitHub Desktop.
Save chiller/377aa5944d620cedb98bcc0ff0a8056b to your computer and use it in GitHub Desktop.
Fish operator
import Control.Monad
import Control.Applicative (Applicative(..))
import Control.Monad (liftM, ap)
family = [("grandpa", Nothing),
("dad", Just "grandpa"),
("son", Just "dad"),
("daughter", Just "dad")
]
children person =
map (\(name, _) -> name)
$ filter (\(name, parent) -> parent == Just person) family
instance Functor Pot where
fmap = liftM
instance Applicative Pot where
pure x = Potato x 0
(<*>) = ap
data Pot a = Potato a Int deriving Show
myfun :: Int -> Pot Int
myfun x = case even x of
True -> Potato (x+1) 1
False -> Potato ( x+1) 0
instance Monad Pot where
return a = Potato a 0
-- fn :: a -> Potato b
-- >>= :: Pot a -> (a -> Pot b) -> Pot b
Potato a b >>= fn = case fn a of
Potato x y -> Potato x (y+b)
-- No instance for (Monad ((,) Int)) arising from a use of ‘<=<’
myfun3 = myfun <=< myfun
class YesNo a where
yesno :: a -> Bool
-- No instance for (YesNo a0) arising from a use of ‘yesno’
instance YesNo [a] where
yesno [] = False
yesno _ = True
x = yesno [5]
main = putStrLn $ show $ (myfun <=< myfun <=< myfun <=< myfun ) 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment