Skip to content

Instantly share code, notes, and snippets.

@nvanderw
nvanderw / typeclass.hs
Created January 31, 2014 22:00
Typeclasses as datatypes
-- Standard Prelude definitions
-- data Ordering = LT | EQ | GT
-- class Eq a => Ord a where
-- compare :: a -> a -> Ordering
isOrdered :: Ord a => [a] -> Bool
isOrdered (x:y:xs) = case compare x y of
GT -> False
_ -> isOrdered (y:xs)
isOrdered _ = True
@nvanderw
nvanderw / Block.elm
Created February 22, 2014 21:54
Bouncing logo demo
import Signal
import Window
(block_width, block_height) = (100, 100)
speed = 1.5
dimensions = Window.dimensions
width = lift fst dimensions
height = lift snd dimensions
@nvanderw
nvanderw / Index.hs
Created March 19, 2014 06:14
Finite sets and safe list indexing
{-# LANGUAGE DataKinds, KindSignatures, GADTs #-}
data Nat = Zero | Succ Nat
-- Bounded naturals
data Fin :: Nat -> * where
FZ :: Fin (Succ n)
FS :: Fin n -> Fin (Succ n)
data Vect (n :: Nat) a where
@nvanderw
nvanderw / EvenOdd.hs
Created March 19, 2014 15:09
Proof that numbers go even-odd-even-odd...
{-# LANGUAGE DataKinds, KindSignatures, GADTs #-}
data Nat = Zero | Succ Nat
data Even :: Nat -> * where
Even_base :: Even Zero
Even_induct :: Even k -> Even (Succ (Succ k))
data Odd :: Nat -> * where
Odd_base :: Odd (Succ Zero)
@nvanderw
nvanderw / PostScript.idr
Last active August 29, 2015 13:57
Proof-of-concept EDSL for writing PostScript with static stack effects.
module Main
data Literal : Type where
float : Float -> Literal
-- A PostScript program which takes a stack of one size to a stack of
-- another size.
-- (PostScript m n) takes a stack of size m and returns a stack of size n.
data PostScript : Nat -> Nat -> Type where
nop : PostScript n n
@nvanderw
nvanderw / gist:11004767
Last active August 29, 2015 14:00
Something catamorphism-like?
newtype Fix f = Fix (f (Fix f))
-- Define List as the fixed point of a type operator
data ListR a t = Nil | Cons a t
type List a = Fix (ListR a)
-- The type operator ListR represents "one layer" of the structure
-- of List. We can do something fold-like with it.
-- My question is, is there a name for f? It's like a catamorphism
-- but doesn't recurse into the data.
@nvanderw
nvanderw / pre-receive
Created June 15, 2012 23:52
A pre-receive hook for blacklisting certain objects
#!/usr/bin/env bash
# Blacklist commit 1abfdd7
root=$(awk '{print $2}')
objects=$(git rev-list --objects $root | awk '{print $1}')
echo $objects | grep 1abfdd7cb8c67ead50757ea596aa0f01b0ab1389 > /dev/null
if [[ $? -eq 0 ]]
then
@nvanderw
nvanderw / gist:2963459
Created June 21, 2012 02:18
Exercise Set 7.1
import System.Environment
sumSquares :: (Integral a) => a -> a
sumSquares n = n*(n+1)*(2*n+1) `div` 6
sumCubes :: (Integral a) => a -> a
sumCubes n = ((n^2 + n) `div` 2)^2
main = do
args <- getArgs
@nvanderw
nvanderw / fibs.hs
Created August 4, 2012 03:08
Polymorphic fibonacci generation using monad transformers
import Control.Monad
import Control.Monad.State.Lazy
-- Simple way to get Fibonacci numbers using monad transformers
fibT :: (Monad m, Num a) => StateT a (StateT a m) ()
fibT = get >>= \x -> lift get >>= \y -> put y >> (lift . put $ x + y)
-- Get the nth Fibonacci number
fib :: Num a => Int -> a
fib n = execState (execStateT (replicateM_ n fibT) 0) 1
@nvanderw
nvanderw / passwords.hs
Created August 30, 2012 18:54
Memorable random words from a dictionary
module Main (main) where
import Control.Monad
import Data.Char
import System.IO
import System.Environment
import System.Random
import System.Exit