Skip to content
Browse files

add letrec

  • Loading branch information...
1 parent 7aa531d commit af940fee546a2d21f36ccfe692d8b94751f4344d @cpylua committed
Showing with 20 additions and 0 deletions.
  1. +7 −0 lib/core.scm
  2. +13 −0 test/let-test.scm
View
7 lib/core.scm
@@ -552,3 +552,10 @@
(else
`(let ,(list (car bindings)) (let* ,(cdr bindings) ,@body))))))
+; letrec
+(define-macro letrec
+ (lambda (bindings . body)
+ `(let ,(map (lambda (b) (list (car b) ''*undefined*)) bindings)
+ ,@(map (lambda (b) (cons 'set! b)) bindings)
+ ,@body)))
+
View
13 test/let-test.scm
@@ -4,3 +4,16 @@
(display-line x)
(display-line y)
(display-line z))
+
+(letrec ((even?
+ (lambda (n)
+ (if (zero? n)
+ #t
+ (odd? (- n 1)))))
+ (odd?
+ (lambda (n)
+ (if (zero? n)
+ #f
+ (even? (- n 1))))))
+ (display-line (even? 88)))
+

0 comments on commit af940fe

Please sign in to comment.
Something went wrong with that request. Please try again.