Skip to content

Instantly share code, notes, and snippets.

@chowells79
chowells79 / tictactoe.hs
Created June 9, 2016 23:50
Tic Tac Toe
{-# LANGUAGE RankNTypes #-}
import Data.Foldable (foldl')
import Data.Monoid (Any)
import Data.Vector (Vector, (!))
import qualified Data.Vector as V
import Control.Lens hiding ((:<))
import Control.Lens.Internal.Context (runPretext)
import Control.Comonad.Cofree
data Ptree a = Pnode a [Ptree a] deriving (Eq, Show)
ptLeaf :: Char -> Ptree Char
ptLeaf c = Pnode c []
ptInsert :: String -> Ptree Char -> Ptree Char
pInsert [] tree = tree
ptInsert (c:cs) (Pnode v children) = case children of
[] -> undefined -- the node has no children
(current:rest) | v == c -> undefined -- the node has children, and the first one is what we're looking for
@chowells79
chowells79 / earleyexpr.hs
Last active January 5, 2019 04:24
Parsing a simple Expr type with the Earley library
{-# LANGUAGE LambdaCase, DeriveFunctor, RecursiveDo #-}
-- parsing
import Text.Earley (Grammar, Prod,
parser, fullParses, rule, satisfy, token, list)
import Control.Applicative ((<|>), some)
import Data.Char (isDigit)
-- recursion schemes
import Data.Functor.Foldable (Fix(..), cata)
@chowells79
chowells79 / AoC0818.hs
Last active December 10, 2018 15:58
Advent of Code 2018 Day 8 -- SPOILERS
import Text.Megaparsec
import Text.Megaparsec.Char (space1)
import Text.Megaparsec.Char.Lexer (decimal)
import Control.Monad (replicateM)
import Control.Lens hiding (children)
import qualified Data.Vector as V
-- data type
data Tree = Tree (V.Vector Tree) (V.Vector Int) deriving (Eq, Show, Ord)
~/hask$ cabal v2-repl --build-depends lens --build-depends megaparsec
Resolving dependencies...
Build profile: -w ghc-8.6.1 -O1
In order, the following will be built (use -v for more details):
- fake-package-0 (lib) (first run)
Configuring library for fake-package-0..
Preprocessing library for fake-package-0..
Warning: No exposed modules
GHCi, version 8.6.1: http://www.haskell.org/ghc/ :? for help
Prelude> :load AoC0818.hs
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as VU
power :: Int -> Int -> Int -> Int
power serial x y = hundreds - 5
where
hundreds = (multed `div` 100) `mod` 10
multed = rackId * added
added = serial + base
base = rackId * y
@chowells79
chowells79 / vecconcat.hs
Created March 3, 2019 18:39
Simplest useful type-level programming
{-# LANGUAGE TypeFamilies, GADTs #-}
{-# LANGUAGE StandaloneDeriving, EmptyDataDecls #-}
data Z
data S n
data Vec n a where
Nil :: Vec Z a
Cons :: a -> Vec n a -> Vec (S n) a
deriving instance Show a => Show (Vec n a)
-- The ancient symbol consisting of a serpent or dragon devouring its
-- own tail. This is the central loop of a BFS implementation that
-- used laziness to share the list of enqueued elements for future
-- processing with the output list of visited elements.
--
-- That implementation detail isn't that important for using it,
-- though. The first argument is a processing function that takes the
-- current element and state, and returns any number of new elements
-- to process along with a new state for processing the next
-- element. The second argument is an initial list of elements to

Class import magic

Demonstrate how to get things to magically happen when you import instances of a class.

{-# Language PolyKinds, GADTs, ScopedTypeVariables, TypeApplications #-}
module HasochistIntersperse (intersperse) where
import Data.Singletons
data Between a v = Empty | Has v deriving (Eq, Ord, Show)
instance forall k (a :: k) v.
(SingI a, SingKind k, v ~ Demote k, Semigroup v) =>