diff --git a/piclib/built-in.scm b/piclib/built-in.scm index 70a36595..03c98548 100644 --- a/piclib/built-in.scm +++ b/piclib/built-in.scm @@ -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))) diff --git a/t/letrec.scm b/t/letrec.scm new file mode 100644 index 00000000..2848ae32 --- /dev/null +++ b/t/letrec.scm @@ -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)))