Skip to content

Instantly share code, notes, and snippets.

@MgaMPKAy
MgaMPKAy / TH.hs
Created May 16, 2012 17:37
Learn some template haskell
{-# LANGUAGE TemplateHaskell #-}
module TH where
import Language.Haskell.TH
import Control.Monad (replicateM)
-- f :: Int -> Int -> Int -> Int
-- $(apply 'f n) :: [Int] -> Int
applyToList f n = do
names <- replicateM n (newName "a")
@MgaMPKAy
MgaMPKAy / Fib.hs
Created May 17, 2012 06:06
Calculate fib n in compile time with TemplateHaskell
{-# LANGUAGE TemplateHaskell #-}
module Fib where
import Language.Haskell.TH
import Language.Haskell.TH.Syntax
fib' :: Int -> Int
fib' n
| n <= 2 = 1
| otherwise = fib' (n - 1) + fib' (n - 2)
@MgaMPKAy
MgaMPKAy / gist:2882040
Created June 6, 2012 14:02
code from The Craft of Functional Programing, chapter 19
import System.IO
import Prelude hiding (lookup)
while test action = do
res <- test
if res
then do action >> while test action
else return ()
copy = while (do {x <- isEOF; return (not x)}) (getLine >>= putStr)
@MgaMPKAy
MgaMPKAy / RPN.hs
Created June 8, 2012 15:54
State Monad example
module RPN where
import Control.Monad
import Control.Monad.State
type Stack = [Int]
type StackState = State Stack
push :: Int -> StackState ()
push n = modify (n:)
@MgaMPKAy
MgaMPKAy / selected.js
Created June 9, 2012 06:22
auto select
for (var i = 2; i < 22; i++) {
$$("#DataGrid1__ctl" + i + "_JS1")[0].selectedIndex = 1;
}
{-# LANGUAGE RankNTypes #-}
module Postfix where
import qualified Stack as Stack
type Cmd s s' = forall a. s -> (s' -> a) -> a
post :: (s -> s') -> Cmd s s'
post f s = next (f s)
module Main where
import Data.List (intersperse)
padding :: String -> Int -> String
padding str n
| length str >= n * 2 = str
| otherwise = let l = (n * 2 - length str) `div` 2
in replicate l ' ' ++ str ++ replicate l ' '
@MgaMPKAy
MgaMPKAy / Alarm.hs
Created June 12, 2012 08:34
thread example code in haskell
module Main where
import Control.Concurrent
import Control.Monad
import System.Exit
data Alarm = Alarm {
second :: Int,
message :: String
}
@MgaMPKAy
MgaMPKAy / Arr.hs
Created June 12, 2012 08:45
Arrow example
{-# LANGUAGE Arrows #-}
module Main where
import Control.Arrow
import Control.Monad
import qualified Control.Category as Cat
import Data.List
import Data.Maybe
import System.Random
import Control.Monad
type Reciver a = [a] -> IO ()
type Sender a = [a]
sender :: Int -> Sender Int
sender n = n : sender (n + 1)
printer :: Show a => Reciver a
printer = mapM_ print