Skip to content

Commit

Permalink
[docs/lang0] nat-even-odd.scm -- by wrap and Y -- instead of direct r…
Browse files Browse the repository at this point in the history
…ecursion
  • Loading branch information
xieyuheng committed Mar 30, 2024
1 parent a142dfd commit 6309b4e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
3 changes: 1 addition & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[docs/lang0] remove `(fix)`
[docs/lang0] remove `(fixpoint)`
[docs/lang0] remove `(fix)` & `(fixpoint)`
[docs/lang0] 用中文重新整理 lambda encoding 相关的知识,形成一本书。
[docs/lang0] 用中文重新整理 lambda encoding 和 self type 相关的知识。
[lang0] 支持直接递归函数与相互递归函数,不能判断等价的地方就不判断。
Expand Down
27 changes: 15 additions & 12 deletions docs/lang0/examples/nat-even-odd.scm
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
(import "./boolean.scm" true false if and or not)
(import "./nat-church.scm" zero add1 sub1 zero?)
(import "./nat-church.scm" one two three four)
(import "./fix.scm" Y)

(define (even? n)
(if (zero? n) true
(if (zero? (sub1 n)) false
(even? (sub1 (sub1 n))))))
(define even?-wrap
(lambda (even?)
(lambda (n)
(if (zero? n) true
(if (zero? (sub1 n)) false
(even? (sub1 (sub1 n))))))))

(define (odd? n)
(if (zero? n) false
(if (zero? (sub1 n)) true
(odd? (sub1 (sub1 n))))))
(define even? (Y even?-wrap))

even?
odd?
(define odd?-wrap
(lambda (odd?)
(lambda (n)
(if (zero? n) false
(if (zero? (sub1 n)) true
(odd? (sub1 (sub1 n))))))))

(assert-equal even? even?)
(assert-equal odd? odd?)
(define odd? (Y odd?-wrap))

(assert-equal
(even? zero)
Expand Down
2 changes: 0 additions & 2 deletions docs/lang0/examples/nat-even-odd.scm.out

This file was deleted.

0 comments on commit 6309b4e

Please sign in to comment.