KRASCHE
&
BYRNE
         ELZED  HOME       NEWS       DOCS       DOWNLOADS       LICENSING       SUPPORT       FAQ       ABOUT  US

What's An Elzed?
Features
Licensing
Downloads
Documentation
Elzed News
    ELZED 
 Documentation 
 The Details 

Recursion

A fairly advanced programming technique, both beautiful and dangerous. A recursive function calls itself. I used to think it was mostly useful for impressing the natives; there is no class of problems that can be solved only with a recursive approach. There are, admittedly, some problems that are easier to grapple with recursion than with conventional methods.

I argued with myself about supporting it in Elzed for some time, before deciding in favor. (So, did I lose the argument? You be the judge.) Two forms of recursion are supported: direct and indirect, both through the expression-based variant of external operators. Obviously, you are free to do as you wish inside your callback operators.

The direct form can be thought of as conventional recursion. An operator calls itself until it detects an end condition, at which point all the calls unwind and the result is returned.

The indirect form is a circular reference. Operator "A" calls operator "B", which then calls operator "A", and so forth until the end condition is detected.

In both cases, the problem is one of definition. The parser will not recognize an operator which has not been defined. If it encounters operator "A" in the course of parsing the expression for operator "A", it will return an error. The same goes for indirect recursion: both operators "A" and "B" must be defined before the parser can handle their supporting expressions. A seeming impossibility.

The answer is a form of partial definition that acts like a C++ "forward declaration", and is accomplished in two steps. Normally one might call lzDefineExpOp to set up an expression-based external operator. To set up a directly recursive operator, make the same call but pass a null in place of the supporting expression. Then make a second call, to lzDefineExpOpStr, passing in the supporting expression. Doing the first step identifies the operator to Elzed so the parser will recognize it. The second step parses the supporting expression and makes the operator ready to use. Call lzReadExpOp and lzReadExpOpStr for file-based expressions.

For indirect recursion, the answer is similar. Perform the first step outlined above for all the operators in the circle, then perform the second step for them all. Note that while the above example uses two operators, in practice you may have as many operators in the circle as you need.

For more information, see the RecursiveDemo function in the sample code.



  Copyright  ©  MMXXIV  by  R R Le Cropane   •   All Rights Reserved   •   Terms of Use   •   Privacy Policy