Skip to content

Instantly share code, notes, and snippets.

View shhyou's full-sized avatar
💭
Alive

shuhung shhyou

💭
Alive
View GitHub Profile
@shhyou
shhyou / match0n1n-1.hs
Last active December 15, 2015 04:50
Examples from 'Defunctionalization at Work' (paper written by Olivier Danvy and Lasse R. Nielsen). Matching the context-free language L := {0^n1^n | n \in N} where N = {0, 1, 2, ...}
-- L = {0^n 1^n | n \in N}
match :: String -> Bool
match s = case trace s of
Just [] -> True
_ -> False
where trace :: String -> Maybe String
trace ('0':xs) = do
remainString <- trace xs
(case remainString of
@shhyou
shhyou / match0n1n-2.hs
Last active December 15, 2015 04:50
CPS transformed version of match0n1n-1.hs
-- The CPS-transformed version of match.
-- https://gist.github.com/suhorng/5204442
match' :: String -> Bool
match' s = trace s null
where trace :: String -> (String -> Bool) -> Bool
trace ('0':xs) k = trace xs $ \remainString ->
case remainString of
'1':xs' -> k xs'
_ -> False
@shhyou
shhyou / match0n1n-3.hs
Created March 20, 2013 14:53
Obtained by defunctionalizing match', thus obtaining a 2-state PDA.
-- Defunctionalizing match' -- we obtain a 2-state push-down automaton!
-- 1) https://gist.github.com/suhorng/5204442
-- 2) https://gist.github.com/suhorng/5204636
data Cont = Cont0
| Cont1 Cont
match'' :: String -> Bool
match'' s = trace s Cont0
where trace :: String -> Cont -> Bool
@shhyou
shhyou / interpreter-original.hs
Last active December 16, 2015 01:29
A toy interpreter for call-by-value lambda calculus. To be transformed to an abstract machine. See A Functional Correspondence between Evaluators and Abstract Machines, by O. Danvy
{-
The original evaluator to be transformed.
See A Functional Correspondence between Evaluators and Abstract Machines, by O. Danvy
www.brics.dk/RS/03/13/BRICS-RS-03-13.pdf
-}
data Value = Var String
| Lambda String Expr
data Expr = Val Value
| Ap Expr Expr
@shhyou
shhyou / interpreter-machine-like.hs
Last active December 16, 2015 01:29
An abstract machine obtained by transforming the toy interpreter for call-by-value lambda calculus at https://gist.github.com/suhorng/5355222. See A Functional Correspondence between Evaluators and Abstract Machines, by O. Danvy
{-
The abstract machine obtained by transforming
the original direct evaluator.
Previous codes: https://gist.github.com/suhorng/5355222
-}
data Value = Var String
| Lambda String Code
@shhyou
shhyou / TIOJ1652.cpp
Created April 25, 2013 14:12
TIOJ 1652, Alchemy. (98建中校內資訊能力競賽(prob3)) http://218.210.35.237:8080/JudgeOnline/showproblem?problem_id=1652
#include <cstdio>
#include <cstring>
#include <algorithm>
char l_exist[10000], r_exist[10000];
int n, m, val[10000], seq[10000];
int main() {
int i, j, k, diff, ans = 0;
scanf("%d", &n);
@shhyou
shhyou / snake-example.py
Created May 9, 2013 17:15
Learning pygame!
import pygame
import sys # exit
import random # randint
from collections import deque
def new_food(snake, surface):
newfood = (random.randint(0, 80)*10, random.randint(0, 60)*10)
# the food shouldn't appear inside the body of the snake or at where we show the score
while newfood in snake or newfood[1] >= 550:
newfood = (random.randint(0, 80)*10, random.randint(0, 60)*10)
@shhyou
shhyou / gist:5765867
Created June 12, 2013 14:38
The Y combinator for applicative-order evaluation and its CPS-transformed version
(define Y
(lambda (f)
(let [(A (lambda (x) (f (lambda (y) ((x x) y)))))]
(A A))))
(define (factF self)
(lambda (n)
(if (= n 0)
1
(* n (self (- n 1))))))
@shhyou
shhyou / gist:5782353
Created June 14, 2013 14:38
Essentials of Programming Langauges 1e
Essentials of Programming Langauges 1e
Daniel P. Friedman, Mitchell Wand, and Christopher T. Haynes
Chapter 1 Tools for Symbolic Programming
1.1 Simple Expressions
1.2 Data Types
1.3 Procedures
1.4 Summary
Chapter 2 Induction, Recursion, and Scope
@shhyou
shhyou / gist:5782426
Created June 14, 2013 14:45
Essentials of Programming Langauges 3e
Essentials of Programming Langauges 3e
Daniel P. Friedman and Mitchell Wand
Chapter 1 Inductive Sets of Data
1.1 Recursively Specified Data
1.2 Deriving Recursive Programs
1.3 Auxiliary Procedures and Context Arguments
1.4 Exercises
Chapter 2 Data Abstraction