Skip to content

Instantly share code, notes, and snippets.

View ytaki0801's full-sized avatar

TAKIZAWA Yozo ytaki0801

View GitHub Profile
(cond-expand
(gauche (import (gauche base) (scheme base) (scheme write)))
(guile (import (srfi srfi-1)))
(chibi (import (srfi 1) (scheme small))))
(define (f x y) (list (list (+ (* (caar x) (caar y))
(* (cadar x) (caadr y)))
(+ (* (caar x) (cadar y))
(* (cadar x) (cadadr y))))
(list (+ (* (caadr x) (caar y))
;;;; referense
;;;; SLIB 3b1-5
;;;; Gauche 0.9.11
;;;; MIT/GNU Scheme 11.2
(define (reduce-right f ridentity ls)
(if (null? ls) ridentity
(let loop ((ridentity (car ls)) (ls (cdr ls)))
(if (null? ls) ridentity
(f ridentity (loop (car ls) (cdr ls)))))))
cons () {
eval CAR"$CNUM"="$1"; eval CDR"$CNUM"="$2";
CONSR="$CNUM.conscell"; CNUM="$((CNUM+1))";
}
car () { eval CARR="\$CAR${1%%.conscell}"; }
cdr () { eval CDRR="\$CDR${1%%.conscell}"; }
TRUE=1; FALSE=0; NULL="empty";
pairp () {
case "$1" in (*.conscell) PAIRPR="$TRUE" ;; (*)
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
extern int yylex(void);
int yyerror(const char *s) { printf("%s\n", s); return 0; }
int node[4096]; int nnum = 1;
@ytaki0801
ytaki0801 / sread-c1-dot.scm
Created February 8, 2022 02:48
Parser for simplest S-expression with dot notation by using 1-character input in Scheme
(define *lh* #f)
(define (get-token)
(define (put-c1 x) (set! *lh* x))
(define (null-c1) (set! *lh* #f))
(define (get-c1) (if *lh* (let ((lh *lh*)) (null-c1) lh) (read-char)))
(define (tstring t) (list->string (reverse t)))
(define (skip-spaces)
(do ((c (get-c1) (get-c1)))
((not (member c (string->list " \n\r"))) (put-c1 c))))
(let loop ((c (get-c1)) (t '()))
@ytaki0801
ytaki0801 / sread-c1.py
Created February 8, 2022 14:20
Parser for simplest S-expression or array-expression of parentheses by using 1-character input in Python
from sys import stdin
LH = False
def get_token():
def put_c1(x): global LH; LH = x
def null_c1(): global LH; LH = False
def get_c1():
if not LH:
try: return stdin.read(1)
except EOFError: pass
@ytaki0801
ytaki0801 / swrap.sh
Last active February 10, 2022 16:47
Parser for simplest S-expression or array-expression of parentheses by using noncanonical 1-character input in POSIX Shell
#!/bin/sh
# One token input function with look-ahead caching
stty -icanon
LF="$(printf \\012)"
get_c1 () {
case "$LH" in ("") GC1R=$(dd bs=1 count=1 2>/dev/null)
;;(*) GC1R="$LH"; LH="" ;;esac
}
skip_spaces () {
@ytaki0801
ytaki0801 / sread-c1.scm
Last active February 21, 2022 17:41
Parser for simplest S-expression by using 1-character input in Scheme
(define *lh* #f)
(define (get-token)
(define (put-c1 x) (set! *lh* x))
(define (null-c1) (set! *lh* #f))
(define (get-c1) (if *lh* (let ((lh *lh*)) (null-c1) lh) (read-char)))
(define (tstring t) (list->string (reverse t)))
(define (skip-spaces)
(do ((c (get-c1) (get-c1)))
((not (member c (string->list " \n\r"))) (put-c1 c))))
(let loop ((c (get-c1)) (t '()))
@ytaki0801
ytaki0801 / ev.py
Last active February 22, 2022 10:40
A Pure LISP Interpreter in Pseudocode-style Python 3
####
#### ev.py: A Pure LISP Interpreter
#### in Pseudocode-style Python 3
####
#### (C) 2022 TAKIZAWA Yozo
#### This code is licensed under CC0.
#### https://creativecommons.org/publicdomain/zero/1.0/
####
#### <examples>
@ytaki0801
ytaki0801 / w23tm-ds.py
Created December 10, 2022 11:41
Wolfram's 2-state 3-symbol Turing Machine
# Wolfram's 2-state 3-symbol Turing Machine
from time import sleep
delta = {1: {0: (1, 1, 2), 1: (2, -1, 1), 2: (1, -1, 1)},
2: {0: (2, -1, 1), 1: (2, 1, 2), 2: (0, 1, 1)}}
ds = {0: ' ', 1: '.', 2: '*'}
tlen = 80
tape, head, state = [0] * tlen, int(tlen / 2) - 9, 1