Created
November 9, 2016 08:52
-
-
Save jneira/529a496d35e7e3fed79b49b805083022 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
module All where | |
import Life | |
import Grass | |
import Sheep | |
import Wolf | |
main = do | |
putStrLn "Grass:" | |
print $ eat Grass [Grass] | |
print $ eat Grass [Sheep] | |
print $ eat Grass [Wolf] | |
putStrLn "Sheep:" | |
print $ eat Sheep [Grass] | |
print $ eat Sheep [Sheep] | |
print $ eat Sheep [Wolf] | |
putStrLn "Wolf:" | |
print $ eat Wolf [Grass] | |
print $ eat Wolf [Sheep] | |
print $ eat Wolf [Wolf] |
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
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-} | |
module Grass where | |
import Life | |
data Grass = Grass deriving Show | |
instance Life Grass where | |
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
{-# LANGUAGE Rank2Types, FlexibleInstances, MultiParamTypeClasses, FunctionalDependencies,OverlappingInstances #-} | |
module Life where | |
type World = (CanEat a b) => [a] -- or something more complex, cells, maps, .. | |
class Life a where | |
class (Life a, Life b) => CanEat a b where | |
canEat :: a -> b -> Bool | |
instance (Life a, Life b) => CanEat a b where | |
canEat _ _ = False | |
eat :: (CanEat a b) => a -> [b] -> [b] | |
eat _ [] = [] | |
eat a (b:bs) = if (canEat a b) then bs else b : eat a bs |
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
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-} | |
module Sheep where | |
import Life | |
import Grass | |
data Sheep = Sheep deriving Show | |
instance Life Sheep where | |
instance CanEat Sheep Grass where | |
canEat _ _ = True |
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
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-} | |
module Wolf where | |
import Life | |
import Sheep | |
data Wolf = Wolf deriving Show | |
instance Life Wolf where | |
instance CanEat Wolf Sheep where | |
canEat _ _ = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment