Church numerals: Difference between revisions

J
(→‎{{header|Julia}}: add Extended Church functions...)
(J)
Line 1,261:
<pre>[7, 12, 81, 64, 1, 0, 3, 8, 3, 4]</pre>
Note that Haskell has directly recursive functions so the y-Combinator is not used to implement recursion in the Church division function.
 
=={{header|J}}==
 
Implementation:
 
<lang J>chget=: {{(0;1;1;1) {:: y}}
 
chset=: {{
'A B'=.;y
'C D'=.B
'E F'=.D
<A;<C;<E;<<.x
}}
 
ch0=: {{
if.0=#y do.y=.;:'>:' end. NB. replace empty gerund with increment
0 chset y`:6^:2`''
}}
 
apply=: `:6
 
chNext=: {{(1+chget y) chset y}}
 
chAdd=: {{(x +&chget y) chset y}}
chSub=: {{(x -&chget y) chset y}}
chMul=: {{(x *&chget y) chset y}}
chExp=: {{(x ^&chget y) chset y}}
int2ch=: {{y chset ch0 ''}}
ch2int=: chget</lang>
 
Task example:
 
<lang J>three=: chNext^:3 ch0''
four=: chNext^:4 ch0''
sixtyfour=: four chExp three
eightyone=: three chExp four
four apply 1
16
chget three
3
chget four
4
chget sixtyfour
64
chget eightyone
81
</lang>
 
Illustration of the difference between a church numeral and the represented functions:
 
<lang J>
three apply 0
3
three apply 10
13
four=: 4 chset ch0 {{2*y}}`''
chget four
4
four apply 0
0
four apply 10
160</lang>
 
=={{header|Java}}==
6,962

edits