新次元新発想!新しい文法を作るスレ
■ このスレッドは過去ログ倉庫に格納されています
0194デフォルトの名無しさん
NGNGLISPで適当なトランスレータ作るとか。
S式と違って、文法的なエラーを早い段階で検出できるから、
素で書いてくよりも効率いいし、append系を演算子にすると楽だよ。
S式自体もread使えばそのまま埋めこめるし。
define flat-tag = lambda(tag, totag, s) =>
let loop (x=s, r='()) in
if pair?(x)then
if tagged-list?(car(x), tag) then
loop(append(cdar(x), cdr(x)), r)
else loop(cdr(x), cons(car(x), r))
else `(,totag ,@(reverse r));
define-macro +s x => flat-tag('+s, 'string-append, x);
define-macro +p x => flat-tag('+p, 'list*, x);
define-macro +l x => flat-tag('+l, 'append, x);
define fib(n)=>if n < 2 then n else fib(n - 1) * fib(n - 2);
define factorial(n)=>if n=0 then 1 else n * factorial(n - 1);
↓
(define flat-tag
(lambda (tag totag s)
(let loop ((x s) (r '()))
(if (pair? x)
(if (tagged-list? (car x) tag)
(loop (append (cdar x) (cdr x)) r)
(loop (cdr x) (cons (car x) r)))
`(,totag ,@(reverse r))))))
(define-macro (+s . x) (flat-tag '+s 'string-append x))
(define-macro (+p . x) (flat-tag '+p 'list* x))
(define-macro (+l . x) (flat-tag '+l 'append x))
(define (fib n) (if (< n 2) n (* (fib (- n 1)) (fib (- n 2)))))
(define (factorial n) (if (= n 0) 1 (* n (factorial (- n 1)))))
■ このスレッドは過去ログ倉庫に格納されています