Skip to content

Instantly share code, notes, and snippets.

@jtobin
jtobin / foo.hs
Created Feb 15, 2016
Independence and Applicativeness
View foo.hs
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE LambdaCase #-}
import Control.Applicative.Free
import Control.Monad
import Control.Monad.Free
import Control.Monad.Primitive
import System.Random.MWC.Probability (Prob)
import qualified System.Random.MWC.Probability as MWC
@jtobin
jtobin / histo-futu.hs
Created Feb 9, 2016
Time-traveling recursion schemes
View histo-futu.hs
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeFamilies #-}
import Control.Comonad.Cofree
import Control.Monad.Free
import Data.Functor.Foldable
oddIndices :: [a] -> [a]
oddIndices = histo $ \case
Nil -> []
@jtobin
jtobin / foo.hs
Created Jan 31, 2016
Using recursion-schemes w/non-functor type
View foo.hs
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeFamilies #-}
import Data.Functor.Foldable hiding (Foldable, Unfoldable)
import qualified Data.Functor.Foldable as RS (Foldable, Unfoldable)
data Expr =
Num Int
| Sum Expr Expr
@jtobin
jtobin / apo.hs
Created Jan 19, 2016
Sorting (Slowly) With Style
View apo.hs
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE TypeFamilies #-}
import Data.Functor.Foldable
data ListF a r =
ConsF a r
| NilF
deriving (Show, Functor)
@jtobin
jtobin / cofree.hs
Created Dec 9, 2015
A program defined using 'Cofree'
View cofree.hs
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
data Program f a = Program {
annotation :: a
, running :: f (Program f a)
}
@jtobin
jtobin / free.hs
Created Dec 9, 2015
A program defined using 'Free'
View free.hs
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
data Program f a =
Running (f (Program f a))
| Terminated a
deriving instance (Show a, Show (f (Program f a))) => Show (Program f a)
@jtobin
jtobin / fix.hs
Created Dec 9, 2015
A program defined using 'Fix'
View fix.hs
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
data Program f = Running (f (Program f))
deriving instance (Show (f (Program f))) => Show (Program f)
data Instruction r =
@jtobin
jtobin / fix-free-cofree.hs
Created Dec 9, 2015
Fix, Free, and Cofree
View fix-free-cofree.hs
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
import Prelude hiding (succ)
newtype Fix f = Fix (f (Fix f))
deriving instance (Show (f (Fix f))) => Show (Fix f)
@jtobin
jtobin / recursion_schemes.hs
Created Sep 6, 2015
An illustration of the recursion-schemes library.
View recursion_schemes.hs
{-# LANGUAGE DeriveFunctor #-}
import Data.List.Ordered (merge)
import Data.Functor.Foldable
import Prelude hiding (Foldable, succ)
data NatF r =
ZeroF
| SuccF r
deriving (Show, Functor)
@jtobin
jtobin / ordered.hs
Created Jul 16, 2015
longest ordered word in a dictionary
View ordered.hs
module Ordered where
import Data.Function (on)
import Data.List (sort, maximumBy)
longestOrdered :: Ord a => [[a]] -> Maybe [a]
longestOrdered dict = safeMaximumBy (compare `on` length)
[word | word <- dict, sort word == word]
safeMaximumBy :: (a -> a -> Ordering) -> [a] -> Maybe a
You can’t perform that action at this time.