Created
February 28, 2021 21:05
-
-
Save Iainmon/087ed7ffe23d7c5bf337e3dcddefa31e to your computer and use it in GitHub Desktop.
Star graph implementation as a functor in Haskell
This file contains hidden or 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
| data Graph a = Vert a [Graph a] deriving (Show) | |
| consStarGraph :: (Num a) => Integer -> a -> Graph a | |
| consStarGraph 0 m = Vert m [] | |
| consStarGraph n m = Vert m edges where | |
| edges = map (\k -> consStarGraph 0 (m + fromInteger k) ) [1..n] | |
| addPoint :: Graph a -> Graph a -> Graph a | |
| addPoint (Vert m edges) point = Vert m (point : edges) | |
| instance Functor Graph where | |
| -- fmap :: (a -> b) -> F a -> F b | |
| fmap f (Vert m []) = Vert (f m) [] | |
| fmap f (Vert m (g:gs)) = Vert (f m) $ map (fmap f) gs | |
| inc = (+1) | |
| main = print $ fmap inc $ consStarGraph 3 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment