Y combinator: Difference between revisions

Content added Content deleted
imported>Arakov
imported>HumptydumptyMe
(This is another take at Y-combinator in Forth.)
Line 3,000: Line 3,000:


=={{header|Forth}}==
=={{header|Forth}}==
<syntaxhighlight lang="forth">\ Address of an xt.
<syntaxhighlight lang="forth">
\ 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
variable 'xt
\ Make room for an xt.
\ Make room for an xt.