Skip to content
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

Recommendation: Mad Pascal Recursion #52

Closed
zbyti opened this issue May 22, 2021 · 4 comments
Closed

Recommendation: Mad Pascal Recursion #52

zbyti opened this issue May 22, 2021 · 4 comments

Comments

@zbyti
Copy link
Collaborator

zbyti commented May 22, 2021

uses crt;

function sum_algo(n: byte): integer;
begin
  result := (n * (n + 1)) shr 1;
end;

//---------------------------------------------

function sum(n: byte): integer;
begin
  if n = 1 then result := 1
  else result := n + sum(n - 1);
end;

//---------------------------------------------

function tail_call(n: byte; i: integer): integer;
begin
  if n = 0 then result := i
  else result := tail_call(n - 1, i + n);
end;

function tail_sum(n: byte): integer;
begin
  result := tail_call(n,0);
end;

//---------------------------------------------

// big n crash the system ;)
begin
  writeln(sum(10));
  writeln(sum_algo(10));

  writeln(tail_sum(20));
  writeln(sum_algo(20));

  repeat until keypressed;
end.

Recursion can go beyond Mad Pascal's software stack. I strongly recommend provide info about this possibility due to small software stack or maybe you can try to implement tail recursion optimization.

@zbyti
Copy link
Collaborator Author

zbyti commented May 22, 2021

But in general I find recursion useless on 8-bit systems ;)

@GSoftwareDevelopment
Copy link
Contributor

Don't take away the ability to create recursion. Allow it, however, let it be known that it has its limitations.

Finally, MAD Pascal, allows you to call other procedures/functions from procedures/functions, repeatedly going deep into the "call tree".
After all, this is the same as recursion, except that different procedures/functions are called.

The call stack is small (I don't know what kind - maybe you can write about it?), but it allows to do that in the simplest form (without optimization).

In general, if recursion is used, it's mostly with a small number of parameters - which is also important for the number of iterations.

I think that recursion is one of the most important features of a language - if you deprive Pascal's MAD of it, it can lose its usefulness.

Translated with www.DeepL.com/Translator (free version)

@zbyti
Copy link
Collaborator Author

zbyti commented May 23, 2021

Most important for me is more space on ZP than ability to use recursion on relatively big (on 8-bit) stack on ZP.

I proposed tail recursion witch is fine on small systems.

@zbyti
Copy link
Collaborator Author

zbyti commented Oct 25, 2021

doesn't matter anymore

@zbyti zbyti closed this as completed Oct 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants