Skip to content

Instantly share code, notes, and snippets.

@kevinmeredith
Created November 13, 2014 01:26
Show Gist options
  • Save kevinmeredith/bbc52c70eb064178501b to your computer and use it in GitHub Desktop.
Save kevinmeredith/bbc52c70eb064178501b to your computer and use it in GitHub Desktop.

Revisions

  1. kevinmeredith created this gist Nov 13, 2014.
    35 changes: 35 additions & 0 deletions JoinList.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    data JoinList m a = Empty
    | Single m a
    | Append m (JoinList m a) (JoinList m a)
    deriving (Eq, Show)

    -- where `m` is a Monoid

    {-# LANGUAGE GeneralizedNewtypeDeriving, FlexibleInstances #-}
    module Sized where

    import Data.Monoid

    newtype Size = Size Int
    deriving (Eq, Ord, Show, Num)

    getSize :: Size -> Int
    getSize (Size i) = i

    class Sized a where
    size :: a -> Size

    instance Sized Size where
    size = id

    -- This instance means that things like
    -- (Foo, Size)
    -- (Foo, (Bar, Size))
    -- ...
    -- are all instances of Sized.
    instance Sized b => Sized (a,b) where
    size = size . snd

    instance Monoid Size where
    mempty = Size 0
    mappend = (+)