mIRC Home    About    Download    Register    News    Help

Print Thread
#227693 18/11/10 07:45 AM
Joined: Dec 2002
Posts: 252
T
Talon Offline OP
Fjord artisan
OP Offline
Fjord artisan
T
Joined: Dec 2002
Posts: 252
sometimes recursion can be a very powerful and handy tool, it'd be nice to have a -r prefix kind of like -l makes them local aliases, -r would allow an alias to be called recursively

there is a way to cheat and make mIRC recursively call an alias.
Code:
alias recurse return $eval($+($,$1,$chr(40),$regsubex($str($chr(44),$calc($0 -2)),//g,$ $+ $calc(\n +1)),$chr(41)),2)


its usage is $recurse(alias to call,$1,$2,$3,etc...)

example solve for greatest common divisor:
Code:
alias gcd return $iif($2 = 0,$1,$recurse(gcd,$2,$calc($1 % $2)))


echo -a $gcd(1920,1080)
result: 120

Talon #227694 18/11/10 08:02 AM
Joined: Oct 2003
Posts: 3,918
A
Hoopy frood
Offline
Hoopy frood
A
Joined: Oct 2003
Posts: 3,918
mIRC explicitly disables recursive calls. It's not a matter of having a switch. The problem is that recursive calls are too easy to screw up (especially when overriding builtin commands), and are generally unnecessary. When they are, you always have the workaround, and having to use a workaround reminds you that you're doing something bad. Having a switch will obscure that, and make recursive aliases seem normal. This begs the question, if recursive aliases are expected to be supported fully, why have a switch to enable them at all? Why not just allow recursive calls period? Of course, I don't think they should be enabled, I just think you'd have better luck if you drop the switch.


- argv[0] on EFnet #mIRC
- "Life is a pointer to an integer without a cast"
argv0 #227703 18/11/10 02:24 PM
Joined: Dec 2002
Posts: 252
T
Talon Offline OP
Fjord artisan
OP Offline
Fjord artisan
T
Joined: Dec 2002
Posts: 252
just because you have problems with recursion doesn't make it a bad thing, if it were, why is it available in just about every language? C++, perl, php, heck I think even Visual Basic allows recursion.

and yes you can usually do the same thing without recursion, but sometimes recursion saves a lot of code.

pseudo code example:

Code:
alias -r gcd return $iif($2 = 0,$1,$gcd($2,$calc($1 % $2)))


compared to a non-recursive version

Code:
alias gcd {
  var %a = $1 , %b = $2
  while (1) {
    %a = %a % %b
    if (%a = 0) return %b
    %b = %b % %a
    if (%b = 0) return %a
  }
}


which would you prefer?


Last edited by Talon; 18/11/10 02:34 PM.
argv0 #227704 18/11/10 03:26 PM
Joined: Apr 2010
Posts: 969
F
Hoopy frood
Offline
Hoopy frood
F
Joined: Apr 2010
Posts: 969
if filtering with the -k switch, you can recursivly call /filter

Code:
/filter -wk @test test *
alias test {
  /filter -wk @test test *
}


So it would appear recursive calls aren't completely disabled...


I am SReject
My Stuff
Talon #227721 19/11/10 01:43 AM
Joined: Oct 2003
Posts: 3,918
A
Hoopy frood
Offline
Hoopy frood
A
Joined: Oct 2003
Posts: 3,918
I'm not speaking on my sole behalf. I was trying to express that this is likely Khaled's viewpoint, as he is the one who disabled recursive calls, not me.

There's also this: Recursive, or iterative?

Most intelligent, well respected, software developers advise against [abusing] recursive problem solving approaches in procedural or imperative programming languages because:

1) function call dispatch will make your implementation slower-- this can be a significant percentage of your runtime (for something as simple as GCD the performance difference is huge), and is why many VM/compilers for functional languages (where recursion is "the right way" due to the paradigm) perform tail-call optimization. Don't expect mIRC to do this.

2) you effectively add another limiting resource: the stack. Your GCD function might look cooler, but it probably won't work on large numbers. The iterative version will always work.

Furthermore, most robust, industry stress-tested implementations will inevitably optimize any recursive implementations into iterative ones where possible, because they are more efficient, and in the end, speed matters more than elegance.

Of course, to answer your question: I'd actually prefer the latter, because I can actually understand it. Elegance != readability. Also note that there are ways to simplify your iterative gcd example into something nearly as elegant and much more readable than the recursive solution.





- argv[0] on EFnet #mIRC
- "Life is a pointer to an integer without a cast"
FroggieDaFrog #227722 19/11/10 01:46 AM
Joined: Oct 2003
Posts: 3,918
A
Hoopy frood
Offline
Hoopy frood
A
Joined: Oct 2003
Posts: 3,918
That is the behaviour of /filter, not directly calling aliases, you're comparing apples and oranges. By the same logic, you can do:

Code:
alias recurse { $1- }
alias foo { recurse foo }


This is completely valid, but it doesn't mean that recursive calls are any less disabled.


- argv[0] on EFnet #mIRC
- "Life is a pointer to an integer without a cast"

Link Copied to Clipboard