add letrec macro

This commit is contained in:
Yuichi Nishiwaki 2013-11-13 18:19:35 +09:00
parent b9fce69c61
commit bc61cc4665
2 changed files with 24 additions and 0 deletions

View File

@ -225,3 +225,10 @@
(eq? '*values-tag* (car res)))
(apply consumer (cdr res))
(consumer res))))
(define-macro (letrec bindings . body)
(let ((vars (map (lambda (v) `(,v #f)) (map car bindings)))
(initials (map (lambda (v) `(set! ,@v)) bindings)))
`(let (,@vars)
(begin ,@initials)
,@body)))

17
t/letrec.scm Normal file
View File

@ -0,0 +1,17 @@
(define (print obj)
(write obj)
(newline)
obj)
(letrec ((my-odd? (lambda (n)
(if (= n 0)
#t
(not (my-even? (- n 1))))))
(my-even? (lambda (n)
(if (= n 0)
#t
(not (my-odd? (- n 1)))))))
(print '(my-odd? 42))
(print (my-odd? 42))
(print '(my-even? 57))
(print (my-even? 57)))