Skip to content

Instantly share code, notes, and snippets.

@TerrorJack
TerrorJack / gist:a61e9515a0320e7d4a4b
Created November 22, 2014 15:12
FP-style list in C++11
#include <memory>
#include <iostream>
template <typename T>
struct _list;
template <typename T>
using list = std::shared_ptr<_list<T>>;
template <typename T>
struct _list {
@TerrorJack
TerrorJack / gist:1fc8f0eb8a8a45f78f14
Last active August 29, 2015 14:10
Memoization in haskell
fib :: Int -> Integer
fib = head . makefib where
makefib 0 = [0]
makefib 1 = [1,0]
makefib n = (head nextfib + head (tail nextfib)):nextfib where nextfib = makefib $ n - 1
main :: IO ()
main = print $ fib 1000
@TerrorJack
TerrorJack / gist:7ea3f024c141ac14425d
Created November 27, 2014 13:50
Handwritten packrat parser for trivial integer arithmetic expressions.
{-
Handwritten packrat parser for trivial integer arithmetic expressions. Guarantees O(n) time/space complexity.
Rule: Exp <- IntVal / (Exp) / (+ Exp Exp) / (- Exp Exp) / (* Exp Exp)
Examples: " 233 ", "( + 42 ((233) ) )", "( (* 42 (+ 1 233)) )".
Properly handles whitespaces. "2 33" will not be recognized as 233.
-}
import Data.Char
data Node = Nil | Node {next::Node,char::Char,skipped::Node,result::Result}
@TerrorJack
TerrorJack / gist:dacc375125c6ff1a38ff
Created December 13, 2014 15:43
Naive PEG parser without memoization
import Data.Char
newtype Parser a = Parser (String -> [(a,String)])
parse :: Parser a -> String -> ([(a,String)])
parse (Parser p) inp = p inp
instance Monad Parser where
return val = Parser (\inp -> [(val,inp)])
pa >>= f = Parser (\inp ->
@TerrorJack
TerrorJack / gist:224b21657cc82f079413
Created December 19, 2014 09:34
Call-by-value lambda calculus interpreter (which won't work)
import qualified Data.Map as Map
type Env = Map.Map String Term
data Term =
VarTerm String
| IntTerm Int
| BoolTerm Bool
| LambdaTerm String Term
| AppTerm Term Term
@TerrorJack
TerrorJack / gist:a413ebced3494e2148a5
Created December 25, 2014 11:53
A simple interpreter (that works). Supports ADT/pattern matching. No type system/callcc/macro.
import qualified Data.Map as Map
type Env = Map.Map String Val
data Exp =
ConstExp Val
| VarExp String
| LambdaExp Pat Exp
| LetrecExp [(Pat,Exp)] Exp
| IfExp Exp Exp Exp
@TerrorJack
TerrorJack / webproxy.json
Created March 14, 2015 07:17
Web proxies. Keys are domains, values indicate country, software and SSL support.
{"0010freeproxy.info": ["DE", "Glype", false], "0013freeproxy.info": ["DE", "Glype", false], "0021freeproxy.info": ["DE", "Glype", false], "007browser.info": ["US", "Glype", false], "123anonymous.com": ["US", "Glype", false], "1774.info": ["US", "Glype", false], "1agent.info": ["US", "Glype", false], "1block.info": ["US", "Glype", false], "1catch.info": ["US", "Glype", false], "1close.info": ["US", "Glype", false], "1closed.info": ["US", "Glype", false], "1force.info": ["US", "Glype", false], "1good.info": ["US", "Glype", false], "1lazy.info": ["US", "Glype", false], "1mesh.info": ["US", "Glype", false], "1proxy.us": ["US", "PHProxy", true], "1total.info": ["US", "Glype", false], "2999.info": ["US", "Glype", false], "2action.info": ["US", "Glype", false], "2closed.info": ["US", "Glype", false], "2fastsurfer.eu": ["US", "Glype", false], "2fix.info": ["US", "Glype", false], "2g23.com": ["US", "Glype", false], "2g34.com": ["US", "Glype", false], "2gram.info": ["US", "Glype", false], "2insta.info": ["US", "Glype"
@TerrorJack
TerrorJack / icfp.json
Created March 25, 2015 16:43
some acm data for fun.
This file has been truncated, but you can view the full file.
{
"event_title": "International Conference on Functional Programming",
"event_contents": [
{
"conference_title": "Commercial Users of Functional Programming",
"conference_contents": [
{
"proceeding_title": "CUFP '10:ACM SIGPLAN Commercial Users of Functional Programming",
"proceeding_contents": [
{
@TerrorJack
TerrorJack / gist:95a964c649c325c8bf87
Last active August 29, 2015 14:22
A minimal PEG parsing library.
import Control.Applicative
import Control.Monad.State
import Data.Char
import Data.Function
type Parser = StateT String Maybe
parse :: Parser a -> String -> Maybe (a,String)
parse = runStateT
@TerrorJack
TerrorJack / gist:7844978a7db17d836d18
Last active August 29, 2015 14:22
By now, only fibState works. All other functions cause memory overflow.
{-
Different ways to implement Fibonacci function in Haskell.
F(0)=0 F(1)=1 F(n)=F(n-1)+F(n-2)
-}
import Control.Monad.State.Strict
import Control.Monad.ST.Strict
import Data.STRef.Strict
import System.TimeIt