Skip to content

Instantly share code, notes, and snippets.

View sasaki-shigeo's full-sized avatar

SASAKI Shigeo sasaki-shigeo

View GitHub Profile
@sasaki-shigeo
sasaki-shigeo / LinearSearchTable.scala
Created October 14, 2021 09:48
A sample code of linear search in Scala
import scala.collection.mutable
class LinearSearchTable[K, V] extends mutable.AbstractMap[K, V] {
protected val table = new mutable.ArrayBuffer[(K,V)]
private def lin_search(key: K): Int = {
var (ix, result) = (0, -1)
while (ix < table.size &&
{ if (key == table(ix)._1) { result = ix; false } else true })
ix += 1
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.Iterator;
import java.util.AbstractMap;
import java.util.AbstractSet;
public class LinearSearchTable<K, V> extends AbstractMap<K, V> {
class Entry extends SimpleEntry<K, V> {
#include <stdlib.h>
#include <stdio.h>
int gcd(int m, int n) {
int r = m % n;
if (r == 0) {
return n;
}
else {
return gcd(n, r);
ArrayList<Star> stars = new ArrayList<Star>();
ArrayList<Crystal> snow = new ArrayList<Crystal>();
void setup() {
size(800, 600);
colorMode(HSB, 360, 1.0, 1.0, 1.0);
for (int i = 0; i < 30; i++) {
stars.add(new Star(random(width), random(0.7 * height), 30));
}
@sasaki-shigeo
sasaki-shigeo / eratosthenes.scm
Created September 16, 2020 18:48
The Sieve of Eratosthenes / エラトステネスの篩
;;;;
;;;; Sieve of Eratosthenes
;;;;
(require srfi/2) ; and-let*
(require srfi/4) ; homogenous numeric vector
(require srfi/42) ; list comprehension
;;;;
;;;;
@sasaki-shigeo
sasaki-shigeo / zermelo-encoding.scm
Created September 6, 2020 15:43
Natural Number Encoding (Zermelo Style)
;;;;
;;;; 自然数の符号化
;;;; ツェルメロ (Zermelo) による方法
;;;;
;;;; ノイマンの方式と比べると,順序の判定をするのに減算が必要なところが不便である。
;;;;
(define (succ x)
(list x))
;;;;
;;;; Primitive Recursive Functions:
;;;;
;;;; ペアノの公理を満たす自然数から,原始再帰関数を使って算術演算を定義したり,
;;;; 原始再帰的でない Hyper演算子や Ackermann 関数を定義する。
;;;;
;;;;
;;;; Peano Axiom:
;;;;
@sasaki-shigeo
sasaki-shigeo / binary-list-encoding.scm
Created September 6, 2020 05:27
Natural Number Encoding by Little Endian Binary List / 二進数リストによる自然数の符号化(LSB を先頭とするので逆順に見える)
;;;;
;;;; little endian binary list style
;;;;
(define (succ xs)
(cond [(null? xs) (cons 1 xs)]
[(= (car xs) 1) (cons 0 (succ (cdr xs)))]
[else (cons 1 (cdr xs))]))
(define zero '())
@sasaki-shigeo
sasaki-shigeo / fraction-encoding.scm
Last active September 6, 2020 06:54
Natural Number Encoding by the series convergent to 1 / 自然数を 1に収束する級数で符号化
;;;;
;;;; s_0 = 0
;;;; s_1 = 1/2
;;;; s_2 = 1/2 + 1/4 = 3/4
;;;; s_3 = 1/2 + 1/4 + 1/8 = 7/8
;;;; ...
;;;; s_n = 1/2 + 1/4 + ... + 1/2^n = (1-2^n)/2^n
;;;;
;;;; この s_n で自然数 n を表すことにするというもの。
;;;; s_ω = 1 となって無限の順序数を考察するのに直観が働きやすいことから,自然数のモデルの例の1つとしてしばしば挙げられる。
@sasaki-shigeo
sasaki-shigeo / church-encoding.scm
Last active September 6, 2020 08:53
Natural Number Encoding (Church Style)
;;;;
;;;; Church encoding
;;;;
;;;; ラムダ計算で自然数を符号化する方法
;;;;
;;;; 基本的アイディア:
;;;; one(f, x) = f(x)
;;;; two(f, x) = f(f(x))
;;;; three(f, x) = f(f(f(x)))
;;;; ...