Skip to content

Instantly share code, notes, and snippets.

View md2perpe's full-sized avatar

Per Persson md2perpe

View GitHub Profile
@isomorphism
isomorphism / RotateArgs.hs
Created July 16, 2011 18:25
generalized flip
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module RotateArgs where
data Z = Z
data S n = S n
class RotateArgs n a where
@palladin
palladin / gist:1088009
Created July 17, 2011 20:13
Hughes's FuncList
// for more info http://www.cs.tufts.edu/~nr/cs257/archive/john-hughes/lists.pdf
type FuncList<'a> = 'a list -> 'a list
// Monoid comprehension
type FuncListBuilder() =
member self.Combine (first : FuncList<'a>, second : FuncList<'a>) : FuncList<'a> = (first << second)
member self.Zero() : FuncList<'a> = id
member self.Yield (value : 'a) : FuncList<'a> = fun tail -> value :: tail
member self.YieldFrom (value : FuncList<'a>) : FuncList<'a> = value
member self.Delay ( f : unit -> FuncList<'a>) : FuncList<'a> = (fun tail -> f () tail)
@palladin
palladin / gist:1088010
Created July 17, 2011 20:14
Hughes's CPSFuncList
type FuncList<'a> = 'a list -> 'a list
type CPSFuncList<'a> = FuncList<'a> -> FuncList<'a>
// Monoid comprehension
type CPSFuncListBuilder() =
member self.Combine (first : CPSFuncList<'a>, second : CPSFuncList<'a>) : CPSFuncList<'a> =
(fun k -> second (fun tail -> first (fun tail' -> k tail') tail))
member self.Zero() : CPSFuncList<'a> = (fun k tail -> k tail)
member self.Yield (value : 'a) : CPSFuncList<'a> = (fun k tail -> k (value :: tail))
member self.YieldFrom (value : CPSFuncList<'a>) : CPSFuncList<'a> = value
@CHH
CHH / .gitignore
Created August 2, 2011 21:09
PHP Templating Engine with bindable $this support in 53 LOC
vendor/
composer.lock
fizzbuzz :: Int -> String
fizzbuzz n = unlines $ map fizzbuzz' [1 .. n]
where
fizzbuzz' n = let p = n `mod` 3 == 0
q = n `mod` 5 == 0
in case (p, q) of
(p, q) | p && q -> "FizzBuzz"
| p -> "Fizz"
| q -> "Buzz"
| otherwise -> show n
-- playing with the adaptive library.
import Control.Monad.Adaptive
printMod mod = do
r <- inCh $ readMod mod
inM (putStrLn $ show r)
main = run $ do
i1 <- newMod (return (2 :: Integer))
@folone
folone / gist:1188506
Created September 2, 2011 12:36
Factorial with type magic. There's more here: http://www.willamette.edu/~fruehr/haskell/evolution.html
-- static Peano constructors and numerals
data Zero
data Succ n
type One = Succ Zero
type Two = Succ One
type Three = Succ Two
type Four = Succ Three
@lest
lest / lolcat.hs
Created September 5, 2011 20:45
simple lolcat powered by haskell
import Data.Word
freq = 0.3
spread = 8.0
unbase :: Integral int => int -> Word8 -> Word8 -> Word8 -> int
unbase base r g b = (fi r*base+fi g)*base+fi b
where fi = fromIntegral
-- | Approximate a 24-bit Rgb colour with a colour in the xterm256 6x6x6 colour cube, returning its index.
@md2perpe
md2perpe / RBTree.hs
Created September 16, 2011 15:58
Using GADTs, phantom types, quantification and empty data declarations to put constraints on nodes in red-black trees
{-# LANGUAGE GADTs, EmptyDataDecls, ExistentialQuantification #-}
module RBTree where
-- Node color
data Red
data Black
-- Node depth
@sjoerdvisscher
sjoerdvisscher / ZigZagPlate.hs
Created October 1, 2011 16:33
Multiplate for the ZigZag data type.
import Control.Applicative
import Data.Functor.Constant
import Data.Generics.Multiplate
data ZigZag a b = ZNil | Zig a (ZigZag a b) | Zag b (ZigZag b a)
data ZigZagPlate a b f = ZigZagPlate
{ znilab :: f (ZigZag a b)
, zigab :: a -> ZigZag a b -> f (ZigZag a b)
, zagab :: b -> ZigZag b a -> f (ZigZag a b)