Skip to content

Instantly share code, notes, and snippets.

View taiki45's full-sized avatar

Taiki Ono taiki45

View GitHub Profile
@taiki45
taiki45 / sample.c
Created July 24, 2012 16:36
C sample
#include <stdio.h>
#define N 7
int min(int* nums, int len);
int max(int* nums, int len);
int first_appear(int* nums, int len, int target);
int main(int argc, char* argv[])
{
int i, len, target;
@taiki45
taiki45 / sts.scm
Created July 22, 2012 16:38
Sample program for STS
(define (enum-nums proc init limit)
(if (> init limit)
'()
(cons init (enum-nums proc (proc init) limit))))
(define (accumulate op init seq)
(if (null? seq)
init
(+ (car seq) (accumulate op init (cdr seq)))))
(accumulate + 0 (enum-nums (lambda (n) (+ 1 n)) 1 100))
@taiki45
taiki45 / sts.c
Created July 22, 2012 16:36
Sample programs for STS
#include <stdio.h>
int sum_to_100(void);
int product_to_10(void);
int main (int argc, const char * argv[])
{
sum_to_100();
product_to_10();
return 0;
@taiki45
taiki45 / 1to99.scm
Created July 12, 2012 08:29
s = 1, 3, ... 99 をすべて足す
(define (make-list proc st en)
(cond ((= st en) (cons (proc en) '()))
(else (cons (proc st)
(make-list proc
(+ st 1)
en)))))
(reduce + 0 (make-list (lambda (n) (+ 1 (* 2 n))) 0 49))
@taiki45
taiki45 / for1to3to99reduce.rb
Created July 10, 2012 12:23
s = 1, 3, ... 99 をすべて足す
# without for
Array.new(50) {|n| 2 * n + 1}.inject(:+)
# with for
s = 0
for i in (0..49) do
s = s + lambda{|n| 2 * n + 1}.call(i)
end
s
@taiki45
taiki45 / map_reduce.scm
Created June 23, 2012 10:44
map and reduce (a.k.a. fold, inject) on Scheme
(reduce + 0 (map (lambda (e) (* e 2)) (list 1 2 3)))
@taiki45
taiki45 / map.scm
Created June 23, 2012 10:02
map on Scheme
(map (lambda (x) (* x x)) (list 1 2 3)) ;=> (1 4 9)
@taiki45
taiki45 / hello.scm
Created June 23, 2012 09:56
Hello World on Scheme
(display "Hello World!\n")