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 TypeFamilies #-} | |
class Graph g where | |
type Vertex g | |
data Edge g | |
src, tgt :: Edge g -> Vertex g | |
outEdges :: g -> Vertex g -> [Edge g] | |
newtype G1 = G1 [Edge G1] | |
instance Graph G1 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 FlexibleInstances #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE TypeFamilies #-} | |
class Add a b where | |
type SumTy a b | |
add :: a -> b -> SumTy a b | |
instance Add Integer Double where | |
type SumTy Integer Double = Double |
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 GADTs #-} | |
data Expr a where | |
I :: Int -> Expr Int | |
B :: Bool -> Expr Bool | |
Add :: Expr Int -> Expr Int -> Expr Int | |
Mul :: Expr Int -> Expr Int -> Expr Int | |
Eq :: Expr Int -> Expr Int -> Expr Bool | |
eval :: Expr a -> a |
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
-- http://blog.sigfpe.com/2010/01/monads-are-trees-with-grafting.html | |
import Control.Applicative | |
import Control.Monad | |
data Tree a = Fork (Tree a) (Tree a) | Leaf a | Nil deriving Show | |
tree1 = Fork (Fork (Leaf 2) Nil) (Fork (Leaf 2) (Leaf 3)) | |
instance Functor Tree where | |
fmap = liftM |
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
-- https://ghc.haskell.org/trac/ghc/wiki/MonadComprehensions | |
{-# LANGUAGE MonadComprehensions #-} | |
import Control.Monad | |
mapl :: Monad m => (a -> m b) -> ([a] -> m [b]) | |
mapl f [] = return [] | |
mapl f (x:xs) = [y:ys | y <- f x, ys <- mapl f xs] |
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
-- http://apfelmus.nfshost.com/articles/monoid-fingertree.html | |
{-# LANGUAGE TypeSynonymInstances #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
import Prelude hiding ((!!), head) | |
import Data.Monoid | |
data Tree v a = Leaf v a | |
| Branch v (Tree v a) (Tree v a) |
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 Prelude hiding (length) | |
data Nested a = a :<: (Nested [a]) | Epsilon | |
infixr 5 :<: | |
nested = 1 :<: [2,3,4] :<: [[4,5],[7],[8,9]] :<: Epsilon | |
length :: Nested a -> Int | |
length Epsilon = 0 | |
length (_ :<: xs) = 1 + length xs |
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 FlexibleContexts #-} | |
data BinaryTree a = Leaf | |
| InternalNode (BinaryTree a) a (BinaryTree a) deriving Show | |
data CompleteBinaryTree a = Leaves | |
| NonLeaves a (CompleteBinaryTree (a,a)) deriving Show | |
data AList a b = Nil | |
| Cons a (AList b a) deriving Show |
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 Prelude hiding (length) | |
newtype Const a b = Const a | |
unConst :: Const c a -> c | |
unConst (Const x) = x | |
length' :: [a] -> Const Int a | |
length' [] = Const 0 | |
length' (x:xs) = Const (1 + unConst (length' xs)) |
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 Data.Maybe | |
ssfold :: (a -> Bool) -> (a -> b -> a) -> a -> [b] -> a | |
ssfold p f a0 xs = foldr (\x xs a -> if p a then a else xs (f a x)) id xs a0 |