add letrec macro
This commit is contained in:
parent
b9fce69c61
commit
bc61cc4665
|
@ -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)))
|
||||
|
|
|
@ -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)))
|
Loading…
Reference in New Issue