add letrec.
This commit is contained in:
parent
1da953b6ab
commit
1584bb8701
8
mes.c
8
mes.c
|
@ -165,6 +165,13 @@ set_cdr_x (scm *x, scm *e)
|
||||||
{
|
{
|
||||||
assert (x->type == PAIR);
|
assert (x->type == PAIR);
|
||||||
x->cdr = e;
|
x->cdr = e;
|
||||||
|
return &scm_unspecified;
|
||||||
|
}
|
||||||
|
|
||||||
|
scm *
|
||||||
|
set_x (scm *x, scm *e, scm *a)
|
||||||
|
{
|
||||||
|
return set_cdr_x (assoc (x, a), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
scm *
|
scm *
|
||||||
|
@ -477,6 +484,7 @@ lookup (char *x, scm *a)
|
||||||
if (!strcmp (x, scm_lambda.name)) return &scm_lambda;
|
if (!strcmp (x, scm_lambda.name)) return &scm_lambda;
|
||||||
if (!strcmp (x, scm_label.name)) return &scm_label;
|
if (!strcmp (x, scm_label.name)) return &scm_label;
|
||||||
if (!strcmp (x, scm_nil.name)) return &scm_nil;
|
if (!strcmp (x, scm_nil.name)) return &scm_nil;
|
||||||
|
if (!strcmp (x, scm_symbol_set_x.name)) return &scm_symbol_set_x;
|
||||||
|
|
||||||
#if QUASIQUOTE
|
#if QUASIQUOTE
|
||||||
if (*x == '`') return &scm_symbol_quasiquote;
|
if (*x == '`') return &scm_symbol_quasiquote;
|
||||||
|
|
17
scm.mes
17
scm.mes
|
@ -75,3 +75,20 @@
|
||||||
(#t
|
(#t
|
||||||
(cond ((pair? else) (car else))
|
(cond ((pair? else) (car else))
|
||||||
(#t *unspecified*)))))
|
(#t *unspecified*)))))
|
||||||
|
|
||||||
|
(define (unspecified-bindings bindings params)
|
||||||
|
(cond ((null? bindings) params)
|
||||||
|
(#t (unspecified-bindings
|
||||||
|
(cdr bindings)
|
||||||
|
(append params (cons (cons (caar bindings) '(*unspecified*)) '()))))))
|
||||||
|
|
||||||
|
(define (letrec-setters bindings setters)
|
||||||
|
(cond ((null? bindings) setters)
|
||||||
|
(#t (letrec-setters (cdr bindings)
|
||||||
|
(append setters
|
||||||
|
(cons (cons 'set! (car bindings)) '()))))))
|
||||||
|
|
||||||
|
(define-macro (letrec bindings . body)
|
||||||
|
(cons 'let (cons (unspecified-bindings bindings '())
|
||||||
|
(append (letrec-setters bindings '())
|
||||||
|
body))) )
|
||||||
|
|
11
test.mes
11
test.mes
|
@ -144,4 +144,15 @@
|
||||||
(display (if (= 1 2) 'true 'false))
|
(display (if (= 1 2) 'true 'false))
|
||||||
(newline)
|
(newline)
|
||||||
|
|
||||||
|
(display 'factorial4=)
|
||||||
|
(display
|
||||||
|
(letrec ((factorial (lambda (n)
|
||||||
|
;; (display 'factorial:)
|
||||||
|
;; (display n)
|
||||||
|
;; (newline)
|
||||||
|
(if (= n 1) 1
|
||||||
|
(* n (factorial (- n 1)))))))
|
||||||
|
(factorial 4)))
|
||||||
|
(newline)
|
||||||
|
|
||||||
'()
|
'()
|
||||||
|
|
Loading…
Reference in a new issue