Jump to content

Church numerals: Difference between revisions

m
→‎{{header|F#}}: correct order of exponent results...
(→‎All closures and a union for type-punning: Nim - add a object variant mostly functional version...)
m (→‎{{header|F#}}: correct order of exponent results...)
Line 593:
[ addChurch c3 c4
; multChurch c3 c4
; expChurch c4 c3
; expChurch c3 c4
; expChurch c4 c3
; iszeroChurch zeroChurch
; iszeroChurch oneChurch
Line 604:
0 // return an integer exit code</lang>
{{out}}
<pre>[7; 12; 6481; 8164; 1; 0; 2; 8; 3; 4]</pre>
Using the Abstract Interface work around is extremely slow, especially for the recursive Church division function which requires many type creations and instantiations such that the execution time for even these small numbers can take many many seconds.
 
Line 661:
[ addChurch c3 c4
; multChurch c3 c4
; expChurch c4 c3
; expChurch c3 c4
; expChurch c4 c3
; isZeroChurch churchZero
; isZeroChurch c3
Line 672:
0 // return an integer exit code</lang>
{{out}}
<pre>[7; 12; 6481; 8164; 1; 0; 2; 8; 3; 4]</pre>
The above code is hundreds of times faster than the previous code taking only a tiny fraction of a second, which shows that using the marshalling kluge as per the previous code isn't necessary and is the wrong way to implement this task.
 
Line 729:
[ addChurch c3 c4
; multChurch c3 c4
; expChurch c4 c3
; expChurch c3 c4
; expChurch c4 c3
; isZeroChurch churchZero
; isZeroChurch c3
Line 740:
0 // return an integer exit code</lang>
{{out}}
<pre>[7; 12; 6481; 8164; 1; 0; 2; 8; 3; 4]</pre>
The above code runs at about the same speed as the Discriminated Union version with side effects.
 
474

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.