Skip to content

Instantly share code, notes, and snippets.

@qzchenwl
qzchenwl / 2011-2012.hs
Created February 23, 2012 14:19
puzzle
module Main where
import Data.Ratio
import Data.Tree
data Label = Label
{ f1 :: (Rational -> Rational)
, accept :: Bool
, f2 :: (Rational -> Rational)
@qzchenwl
qzchenwl / simple-schema.hs
Created February 26, 2012 10:42
implement sub schema in haskell
{-# LANGUAGE ExistentialQuantification #-}
module Main where
import Debug.Trace(traceShow)
import System.Environment
import Text.ParserCombinators.Parsec hiding (spaces)
import Control.Applicative ((<$>))
import Control.Monad.Error
import Data.IORef
import IO hiding (try)
@qzchenwl
qzchenwl / lazy_vs_st.hs
Created March 26, 2012 17:17 — forked from yihuang/lazy_vs_st.hs
lazy vs ST
{-# LANGUAGE BangPatterns, ViewPatterns #-}
import System.Environment (getArgs)
import Control.Monad
import Control.Monad.ST
import Data.STRef
import GHC.Int
fib1 :: Int -> Integer
fib1 n = fibs !! n
where fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
@qzchenwl
qzchenwl / lazy_vs_st.hs
Created March 27, 2012 03:43 — forked from yihuang/lazy_vs_st.hs
lazy vs ST
{-# LANGUAGE BangPatterns #-}
module Main (fib1, fib2, fib3, fib4, main) where
import Control.Monad
import Control.Monad.ST
import Data.STRef
import Data.List (transpose)
import Criterion.Main
fib1 :: Int -> Integer
fib1 n = fst $ fib' n
@qzchenwl
qzchenwl / rsa.hs
Created April 2, 2012 17:35
simple RSA implementation
module RSA where
-- extendedGcd(a,b) = (x,y), such that a*x + b*y = gcd(a,b)
extendedGcd :: Integral a => (a, a) -> (a, a)
extendedGcd (a, b) = if b == 0 then (1, 0) else (x, y)
where
(q, r) = divMod a b
(s, t) = extendedGcd (b, r)
(x, y) = (t, s - q * t)
{-# LANGUAGE BangPatterns #-}
module Main where
import Data.List (transpose)
import Criterion.Main
naiveFib, cachedFib, cachedFib', tailrFib, tailrFib', matrixFib
:: Integer -> Integer
naiveFib 0 = 0
@qzchenwl
qzchenwl / OAuth2.hs
Created April 10, 2012 12:34
Simple oauth2.0 implementation in Haskell
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveDataTypeable #-}
import Data.Aeson
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BSL
import Data.ByteString.Lazy (toChunks)
import Data.List
import Data.Maybe
import Data.Typeable (Typeable)
@qzchenwl
qzchenwl / Indexable.hs
Created April 12, 2012 03:05
type class Indexable
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
module Indexable where
import qualified Data.Aeson as A
import qualified Data.HashMap.Strict as HM
import qualified Data.Map as M
import qualified Data.Vector as V
import Data.Text (Text, unpack)
@qzchenwl
qzchenwl / Church.hs
Created April 18, 2012 14:10
Church Number
module Church where
import Prelude ((.), ($), id, fst, undefined, length)
zero f = id
inc n f = f . n f
one f = f
two f = f . f
three f = f . f . f
module Main where
import Data.Char (digitToInt)
sort :: (Ord a) => [a] -> [a]
sort [] = []
sort (x:xs) = sort smaller ++ [x] ++ sort greater
where
smaller = filter (not.(> x)) xs
greater = filter (> x) xs