0

How to count all the permutations of a string in Microsoft Small Basic?

The brute-force exploring of all permutations is usually done by recursion in languages such as C and C++. However, Microsoft Small Basic doesn't support the arguments for functions, so it's impossible to implement the recursive algorithm the same way.

Perhaps it's doable in Small Basic using the Stack? How exactly?

FlatAssembler
  • 667
  • 7
  • 30

1 Answers1

-1

You can't use arguments in functions in Smallbasic, but since all variables are global, you can simply set them before you call the function and use them in the function. It is also possible for a function to call itself. This means that you can use functions (or subroutines as they are called in SB) for 'brute forcing' this algorithm.

See here:

Sub Printx
  TextWindow.WriteLine(x)

  x = x + 1
  Printx()
EndSub

x = 1
Printx()

Note that this way of doing things may crash the program after around 2,000 'recalls' of the subroutine as it throws a stackoverflow error.

Zock77
  • 951
  • 8
  • 26
  • So, how do you translate this C function to SmallBasic: int fib(int n) {if (!n || n==1) return n; return fib(n-1)+fib(n-2);} See the problem with that "you don't need function arguments when all the variables are global"? To simulate such a simple recursive function, you need to use a stack. – FlatAssembler Dec 27 '17 at 07:55
  • I suppose you are right about that. You should check out the LitDev extension for SB, as I'm sure it could be done with that. http://litdev.co.uk/ – Zock77 Dec 27 '17 at 16:35
  • What do you mean? Deciding whether a variable will be local or global is, as far as I'm aware, done by the part of the compiler called the parser. Linking an executable with another DLL (like the LitDev) can't affect the parser, it happens way after the parsing has already finished. – FlatAssembler Dec 28 '17 at 01:52
  • I mean that it may be possible by using preexisting functions from the LitDev extention. Like this one perhaps? LDCall.Function(funcName, arg1) Or (Slightly cheating) LDInline.IncludeCS(source, assemblies, dllName) – Zock77 Dec 28 '17 at 15:03
  • Can you show me some example code? Like, how to implement the Fibonacci Sequence recursively using the LDCall object? – FlatAssembler Dec 29 '17 at 07:31