作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Rosetta Code Forth 中没有 Y-combinator 的实现。
我怎样才能做到这一点?如何在 Forth 中使用 Y-combinator?为什么?
最佳答案
这是我对 Y 组合器的尝试。申请时y
到一个 xt,你得到另一个 xt 回来。当你执行这个新的 xt 时,它会执行第一个 xt 并传入第二个 xt。
\ Address of an xt.
variable 'xt
\ Make room for an xt.
: xt, ( -- ) here 'xt ! 1 cells allot ;
\ Store xt.
: !xt ( xt -- ) 'xt @ ! ;
\ Compile fetching the xt.
: @xt, ( -- ) 'xt @ postpone literal postpone @ ;
\ Compile the Y combinator.
: y, ( xt1 -- xt2 ) >r :noname @xt, r> compile, postpone ; ;
\ Make a new instance of the Y combinator.
: y ( xt1 -- xt2 ) xt, y, dup !xt ;
\ Count down from 10; passed to the anonymous definition.
10
\ Anonymous definition which recursively counts down.
:noname ( u xt -- ) swap dup . 1- ?dup if swap execute else drop then ;
\ Apply the Y combinator and execute the result.
y execute
\ Should print 10 9 8 7 6 5 4 3 2 1.
RECURSE
, 即使在
:NONAME
定义。
关于computer-science - 如何在 Forth 中实现 Y-combinator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35199836/
我是一名优秀的程序员,十分优秀!