-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recursion #1
Comments
Right now, toolboxforth only supports tail recursion. Effectively, for a tail recursive call to work it has to be the last word in the flow of execution. It essential compiles to a "goto" (jmp), so * is never executed. I've seen an implementation of Forth that looks ahead to see if there is a terminating word (e.g. ";" "then", etc) and does the optimization, otherwise does a normally recursive call. I've never had a use for normal recursion in Forth, but tail calls can actually be faster than begin again loops. |
One way to do normal recursion would be: |
Added a more traditional recursion capability to util.f: So, you can now |
Sorry to reopen, but I tried it: I am using recursion often in forth, but seldom double recursion. So: thank you! |
Ah! My bad. The problem is in my skipping of character packed words. That "fact" worked and "fib" didn't is due to my bad character skipping math (3 vs 4 character words). Fix checked into util.f |
Recursion does'nt work. E.g:
: fact dup 2 > if dup 1- fact * then ;
5 fact .
allways gives 2
However: very simple recursion is okay:
variable n
0 n !
: tst n @ 10 < if n @ . 1 n +! tst then ;
tst
Any suggestion?
The text was updated successfully, but these errors were encountered: