å é¨defineãä½ã
俺Lispãå é¨defineã«å¯¾å¿ããã¦ã¿ãã
repl> (letrec () (define x 1) (define y 2) (cons x y))
http://kar.s206.xrea.com/lisp/?e=(letrec%20()%20(define%20x%201)%20(define%20y%202)%20(cons%20x%20y))
(1 . 2)
以ä¸ã®ãããªå±éå¦çããã¦ãã
(letrec () (define x 1) (define y 2) (cons x y)) â (letrec ((x 1)) (define y 2) (cons x y)) â (letrec ((y 2) (x 1)) (cons x y)) â (let ((y #f) (x #f)) (set! y 2) (set! x 1) (cons x y)) â ((lambda (y x) (set! y 2) (set! x 1) (cons x y)) #f #f)
letrecã¨lambdaã®ã³ã¼ã
(define (eq-car? xs val) (and (pair? xs) (eq? (car xs) val))) (define-macro (letrec arg . body) (if (eq-car? (car body) 'define) `(letrec ,(cons (cdr (macroexpand (car body))) arg) ,@(cdr body)) `(let ,(map (lambda (x) (list (car x) #f)) arg) ,@(map (lambda (x) (list 'set! (car x) (cadr x))) arg) ,@body))) (define-macro (lambda arg . body) (if (eq-car? (car body) 'define) `(new Closure ,arg (letrec () ,@body)) `(new Closure ,arg ,@body)))
lambdaããã¯ãã«ãªã£ãããã«ï¼ããªããã£ãã®(lambda ...)ã®æ©è½ã¯(new Closure ...)ã§å¼ã³åºãããã«ããã
ã¡ãªã¿ã«ï¼(new Macro ...)ãå¾æ¥ã®(macro ...)ç¸å½ã