Jensen's Device: Difference between revisions
Content added Content deleted
m (S>F is now part of the ANS Forth standard) |
(→{{header|Forth}}: added version that passes i and 1/i as execution tokens) |
||
Line 344: | Line 344: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
This version passes i on the stack: |
|||
<lang forth>: sum 0 s>f 1+ swap ?do i over execute f+ loop drop ; |
<lang forth>: sum 0 s>f 1+ swap ?do i over execute f+ loop drop ; |
||
:noname s>f 1 s>f fswap f/ ; 1 100 sum f.</lang> |
:noname s>f 1 s>f fswap f/ ; 1 100 sum f.</lang> |
||
Output: 5.18737751763962 |
Output: 5.18737751763962 |
||
The following version passes i and 1/i as execution tokens and is thus closer to the original, but less idiomatic: |
|||
<lang forth>fvariable ii \ i is a Forth word that we need |
|||
: sum ( xt1 lo hi xt2 -- r ) |
|||
0e swap 1+ rot ?do ( addr xt r1 ) |
|||
i s>f over execute f! dup execute f+ |
|||
loop 2drop ; |
|||
' ii 1 100 :noname 1e ii f@ f/ ; sum f.</lang> |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |