Skip to content

Instantly share code, notes, and snippets.

@yamasushi
yamasushi / pre2i.scm
Last active November 23, 2023 05:49
prefix --> infix
; prefix notation ---> infix notation
; https://gist.github.com/yamasushi/2f940393e46de5c4f90422047f8ce267
(define-module pre2i
(export prefix->infix))
(select-module pre2i)
; S -> <op> S S | <digit>
(define digit? #[0-9])
@yamasushi
yamasushi / post2i.scm
Last active November 23, 2023 05:53
postfix --> infix
; postfix notation --> infix notation
; Compilers 1st ed. ex. 2.8 (p.79)
; Compilers 2nd ed. ex. 2.3.2 (p.60)
; https://gist.github.com/yamasushi/a333611fa259b7f60f0984ba98f5b3ec
(define-module post2i
(export postfix->infix))
(select-module post2i)
; S --> S S <op> | <digit>
@yamasushi
yamasushi / r2i.scm
Last active November 21, 2023 21:12
roman numeral --> integer
; roman numeral --> integer
; Compilers 2nd ed. ex. 2.3.4 (p. 60)
; https://gist.github.com/yamasushi/379df533cd0d58e8b56e2080f2fee95e
(define-module r2i
(export roman->integer))
(select-module r2i)
;-----------------------------------------------
; roman numeral ---> integer
@yamasushi
yamasushi / i2r.scm
Last active November 21, 2023 21:11
intger --> roman numeral
; integer --> roman numeral
; Compilers 1st ed. ex. P2.1 (p. 81)
; Compilers 2nd ed. ex. 2.3.3 (p. 60)
; https://gist.github.com/yamasushi/f0febd8f53c34e4c97a025d1691a4d1d
(define-module i2r
(export integer->roman))
(select-module i2r)
(define num-table #(
@yamasushi
yamasushi / binnum.scm
Last active November 18, 2023 05:53
binary number
; positive binary number
; https://gist.github.com/yamasushi/bb2728ca917c677eea1b6aa07f180358
; Compilers 2nd ed. ex. 5.4.3 (p. 337)
; B -> B1 0 { B.syn = 2 * B1.syn }
; | B1 1 { B.syn = 2 * B1.syn + 1}
; | 1 {B.syn = 1}
; B -> 1 {R.inh = 1} R {B.syn = R.syn}
; R -> 0 {R1.inh = R.inh * 2} R1 {R.syn = R1.syn}
@yamasushi
yamasushi / syntaxtree.scm
Last active November 13, 2023 06:42
syntax tree
gosh$ (tree '(2 ^ < 6 - 7 > + 3 * 4))
(+ (^ 2 (- 6 7)) (* 3 4))
@yamasushi
yamasushi / calc-main.scm
Last active December 16, 2023 03:41
calculator
; test calc
; https://gist.github.com/yamasushi/c956ae30eb20adef2c7c7afd0b70311a
(load "./lex")
(import lex)
(load "./calc")
(import calc)
(use gauche.generator)
@yamasushi
yamasushi / infix2postfix.scm
Last active November 13, 2023 06:40
infix --> postfix
gosh$ (i2p '(2 ^ < 6 - 7 > + 3 * 4))
(6 7 - ^ 3 4 * +)
%% Erlang Programming(Francesco, Cesarini & Simon Thompson)
%% p.212, Exercise 9-5: Existing High-Order Functions
%% https://gist.github.com/yamasushi/b051d2ee88cfaa04dddc0fd2e05db01b
-module(hof).
-compile(export_all).
all(_P, []) -> true;
all(P, [H|T]) -> P(H) and all(P, T).
any(_P, []) -> false;
%% Erlang Programming(Francesco Cesarini & Simon Thompson)
%% P.197, Lazy Evaluation and Lists
%% https://gist.github.com/yamasushi/65b93369fed6c056470ee6e88974be55
-module(lseq).
-compile(export_all).
%% for test
dump(Lseq) ->
lseq:foreach(fun (X)->io:format("~w~n",[X]) end, Lseq).