Skip to content

Instantly share code, notes, and snippets.

View nyuichi's full-sized avatar

Yuichi Nishiwaki nyuichi

View GitHub Profile
@nyuichi
nyuichi / tco.clj
Created May 19, 2011 15:44
Tail Call Optimization in Clojure
(defn tco
[func]
(let [unique-id (gensym)
firstcall (ref true)
arguments (ref nil)
continue? (ref true)
answer (ref nil)]
(fn
[& args]
(if @firstcall
@nyuichi
nyuichi / sisoku.cpp
Created May 25, 2011 13:05
Sisoku Solver in C++
#include <cstdio>
#include <vector>
using namespace std;
int gcd(int a, int b)
{
if(a < b) gcd(b, a);
if(b == 0) return a;
return gcd(b,a%b);
@nyuichi
nyuichi / sisoku.clj
Created May 25, 2011 13:44
Sisoku@clojure
(use 'clojure.contrib.combinatorics)
; How to use
; (sisoku [7 7 7 9 11 11] 218/100)
; (sisoku [1 1 6 11 12 13] 314/100)
(defn drop-nth [n coll]
(concat (take n coll)
(drop (inc n) coll)))
@nyuichi
nyuichi / sisoku.clj
Created May 25, 2011 14:58
Sisoku@Clojure with formatting
(use 'clojure.contrib.combinatorics)
; How to use
; (sisoku-solve [7 7 7 9 11 11] 218/100)
; (sisoku-solve [1 1 6 11 12 13] 314/100)
(defn drop-nth [n coll]
(concat (take n coll)
(drop (inc n) coll)))
@nyuichi
nyuichi / 90-min-scc.scm
Created July 31, 2011 10:36
The 90 Minute Scheme to C Compiler
#!/usr/local/Gambit-C/bin/gsi
; Copyright (C) 2004 by Marc Feeley, All Rights Reserved.
; This is the "90 minute Scheme to C compiler" presented at the
; Montreal Scheme/Lisp User Group on October 20, 2004.
; Usage with Gambit-C 4.0:
;
; % ./90-min-scc.scm test.scm
(use srfi-1)
(define (normalize form)
(cond
[(atom? form) form]
[(and (list? form) (macro? (car form))) form]
[(list? form)
(case (car form)
((define) (normalize-define form))
@nyuichi
nyuichi / LazyK.py
Created January 30, 2012 14:47
LazyK (incomplete)
import re, sys
def lex(s):
for m in re.finditer(r'S|I|K|\(|\)', s):
yield m.group(0)
def parse(s):
l = lex(s)
while True:
x = next(l)
(def fib (n)
(if (< n 2)
1
(+ (fib (- n 1)) (fib (- n 2)))))
(fib 30)
-------------------------------------------
[0] const 0x79 ; fixnum(30)
(def sum (list)
(if (nilp list)
0
(+ (car list)
(sum (cdr list)))))
(sum '(1 2 3))
-----------------------------------------------
@nyuichi
nyuichi / hello.clj
Created March 7, 2012 06:07
clojureのマクロ展開
(ns hello
(:gen-class))
(def x (atom 2))
(defmacro test-macro []
(println @x))
(swap! x inc)