Integrated Application Platform › Forums › General › Heaps Algorithm › Reply To: Heaps Algorithm
August 19, 2017 at 7:50 am
#1324
Keymaster
Hi Ajith,
That is often awkward with recursive functions. There are several options.
One is to have default values for the extra parameters. However, default values must be constants so you need to do something like:
function (ob, size = false, result = false)
{
if size is false
size = ob.Size()
if result is false
result = Object()
This is the simplest, but the extra parameters could confuse users.
Another option is to use two functions but to bundle them together, either using a class:
class
{
CallClass(ob)
{
return .permute(ob, ob.Size(), Object())
}
permute(ob, size, result)
{
... // recursively calling .permute
}
}
CallClass is special so this can still be called like a function.
Or you can use a block:
function (ob)
{
permute = {|ob, size, result|
... // recursively calling permute
result // without 'return' because that would return from outer function
}
return permute(ob, ob.Size(), Object())
}