Church numerals: Difference between revisions

→‎{{header|R}}: added Extended Church Functions...
(→‎{{header|Chapel}}: better Extended Church Function version that uses recursive types and no side effects...)
(→‎{{header|R}}: added Extended Church Functions...)
Line 2,452:
> churchToNat(succAltAlt(four))
[1] 5</pre>
 
===Extended Church Functions in R===
<lang rsplus>const <- function(f) {function(d) f}
zero <- const(identity)
one <- identity
succ <- function(n) {function(f) {function(x) f(n(f)(x))}}
add <- function(n) {function(m) {function(f) {function(x) m(f)(n(f)(x))}}}
mult <- function(n) {function(m) {function(f) m(n(f))}}
expt <- function(n) {function(m) m(n)}
iszero <- function(n) n(const(zero))(one)
pred <- function(n) {function(f) {function(x)
n(function(g) {function(h) h(g(f))})(const(x))(identity)}}
subt <- function(m) {function(n) n(pred)(m)}
divr <- function(n) {function(d)
(function(v) v(const(succ(divr(v)(d))))(zero))(subt(n)(d))}
div <- function(dvdnd) {function(dvsr) divr(succ(dvdnd))(dvsr)}
natToChurch <- function(n) {if(n == 0) zero else succ(natToChurch(n - 1))}
churchToNat <- function(n) {(n(function(x) x + 1))(0)}
three <- natToChurch(3)
four <- succ(three)
eleven <- natToChurch(11)
twelve <- succ(eleven)
churchToNat(add(three)(four))
churchToNat(mult(three)(four))
churchToNat(expt(three)(four))
churchToNat(expt(four)(three))
churchToNat(iszero(zero))
churchToNat(iszero(three))
churchToNat(pred(four))
churchToNat(pred(zero))
churchToNat(subt(eleven)(three))
churchToNat(div(eleven)(three))
churchToNat(div(twelve)(three))</lang>
{{out}}
<pre>[1] 7
[1] 12
[1] 81
[1] 64
[1] 1
[1] 0
[1] 3
[1] 0
[1] 8
[1] 3
[1] 4</pre>
 
=={{header|Racket}}==
474

edits