Skip to content

Instantly share code, notes, and snippets.

View weskerfoot's full-sized avatar

Wesley Kerfoot weskerfoot

View GitHub Profile
@weskerfoot
weskerfoot / sbphi.hs
Created October 17, 2012 23:42
Fun with Stern-Brocot tree
import Control.Monad
import Data.Ratio
data FakeRatio = FakeRatio { numr :: Int, denom :: Int} deriving (Show)
data SBTriple = SBTriple { left :: FakeRatio,
mid :: FakeRatio,
right :: FakeRatio}
data Directions = TLeft | TRight deriving (Show)
@weskerfoot
weskerfoot / lsystem.hs
Created October 20, 2012 00:41
L-System
import Control.Monad
data LSymbol = LRule Char | LDeriv String
type Alphabet = [LSymbol]
type Axiom = [LSymbol]
-- a production is a finite mapping of LSymbol -> LSymbol
-- if no production exists for a given LSymbol on the LHS of a Production
@weskerfoot
weskerfoot / perms.hs
Created November 1, 2012 03:48
Haskell - permutations
import Control.Monad
enumerate xs = zip [1..] xs
dlists :: [a] -> [(a, [a])]
dlists xs = [(x, [y | (j, y) <- enumerate xs, j /= i]) | (i, x) <- enumerate xs]
permute (x:y:[]) = [y:x:[], x:y:[]]
permute xs = let difflist = dlists xs
in join [map (i:) (permute d) | (i, d) <- difflist]
@weskerfoot
weskerfoot / lsys.hs
Created November 28, 2012 21:39
L-Systems
module LSystem where
import Control.Monad
data LSymbol = LRule Char | LDeriv String
type Alphabet = [LSymbol]
type Axiom = [LSymbol]
@weskerfoot
weskerfoot / lgraphs.hs
Created November 28, 2012 21:40
lsysgraphics.hs
{-# LANGUAGE GADTs, FlexibleInstances #-}
import Graphics.Gloss
import Graphics.Gloss.Data.Vector
import LSystem
data PlantInstruction a where
DrawForward :: PlantInstruction a
TurnLeft :: (Show a, Num a) => a -> PlantInstruction a
TurnRight :: (Show a, Num a) => a -> PlantInstruction a
@weskerfoot
weskerfoot / ngrams.hs
Created January 20, 2013 04:49
ngram model
import Data.List.Split
import Data.Char
import qualified Data.Map as Dm
import Control.Applicative
ngrams' n len xs = let next = (take n xs)
in case len == n of
False -> (toLower <$> next) : (ngrams' n (len - 1) $ drop 1 xs)
_ -> return xs
ngrams n xs = ngrams' n (length xs) xs
@weskerfoot
weskerfoot / generalperms.hs
Created January 22, 2013 19:21
generalizes permutations
select [] = []
select (x : xs) = (x, xs) : map (fmap (x :)) (select xs)
perm2 ks = [[x,y] | (x, ys) <- select ks, y <- ys]
permute 2 xs = perm2 xs
permute n xs = join [[x : p | p <- (permute (n-1) ys)] | (x,ys) <- select xs]
import Control.Monad
data AlienSymbol = (:@:) | (:#:) | (:!:) | (:^:) | (:%:) | (:*:) | (:&:) deriving (Ord, Eq, Enum)
data AlienNumber = AN [AlienSymbol] deriving (Ord, Eq)
trim n = let dropped = dropWhile (==(:@:)) n in
case dropped of
[] -> [(:@:)]
_ -> dropped
@weskerfoot
weskerfoot / pe9.hs
Created January 30, 2013 22:45
pe9
import Control.Monad
coprime a b = gcd a b == 1
istriple m n = coprime m n && odd (m - n) && (m > n) && m > 0 && n > 0
triple m n k =
let a = k * (m^2 - n^2)
b = k * (2*m*n)
c = k * (m^2 + n^2)
in [a,b,c]
@weskerfoot
weskerfoot / eratosthenes.hs
Created February 2, 2013 21:14
True sieve of Eratosthenes in Haskell
-- my implementation of this: http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf
import qualified Data.PSQueue as DQ
insertThenDel k p pq = DQ.deleteMin $ DQ.insert k p pq
minPriority pq =
case DQ.findMin pq of
(Just m) ->
let mp = DQ.prio m