Jump to content

Y combinator: Difference between revisions

This is another take at Y-combinator in Forth.
imported>Arakov
imported>HumptydumptyMe
(This is another take at Y-combinator in Forth.)
Line 3,000:
 
=={{header|Forth}}==
<syntaxhighlight lang="forth">\ Address of an xt.
\ Begin of aproach. Depends on 'latestxt' word of GForth implementation.
 
: self-parameter ( xt -- xt' )
>r :noname latestxt postpone literal r> compile, postpone ;
;
: Y ( xt -- xt' )
dup self-parameter 2>r
:noname 2r> postpone literal compile, postpone ;
;
</syntaxhighlight>Usage:<syntaxhighlight lang="forth">
\ Fibonnacci test
10 :noname ( u xt -- u' ) over 2 < if drop exit then >r 1- dup r@ execute swap 1- r> execute + ; Y execute . 55 ok
\ Factorial test
10 :noname ( u xt -- u' ) over 2 < if 2drop 1 exit then over 1- swap execute * ; Y execute . 3628800 ok
 
\ End of aproach.
</syntaxhighlight><syntaxhighlight lang="forth">\ Address of an xt.
variable 'xt
\ Make room for an xt.
Cookies help us deliver our services. By using our services, you agree to our use of cookies.