I am trying to make a simple recursive model. For example, I will call a function and it will recurse until a set of conditions are met.
After reading some advice in the primer, it seems like script in a code block or python are both good options, and so I’m starting with code blocks.
I am having trouble implementing if/then inside the a simple block. I found this example in forums but it is throwing errors:
in code blocks you can use if and else as well, if (condition) { // do this thing } else{ //do that thing } you may need to put this inside an imperative block like result = [imperative]{ a = 0; if(100<200){ a = 10; } return = a; } (not tested)!
I’ve tried a bunch of variations, I don’t really understand what the “imperative” command means or how it works inside another function and can’t seem to find any documentation.
[Imperative] (note that the word begins with an Upper case ‘I’)
A recursive function is one where a function calls itself within it’s definition. This isn’t possible in Design Script. But, it doesn’t seem like you’re looking for that functionality.
Actually = (following return) is optional in the more recent versions of Dynamo
def OddEven(min,max)
{
result = [Imperative]
{
a = [];
c = 0;
for (i in min..max)
{
if (i % 2 == 0)
{
a[c] = i + " is Even";
c = c + 1;
}
else
{
a[c] = i + " is Odd";
c = c + 1;
}
}
return a;
}
return result;
};
can you only define a recursive function in imperative mode? I read through the DS specification and the language guide, and it doesn’t seem to address this. I’m trying to get better at using DesignScript and i’m writing this same factorial function
An If statement can only be used inside an Imperative block. You can still use the DS shorthand version but for some reason it doesn’t return the same error when outside an Imperative block.
maybe this is better as it’s own topic. I’m not sure how to process your response because i’m not trying to use an If statement. I’m using the inline conditional statement to try and make a recursive call in the associative mode, but the dynamo doesn’t finish running. I’m not seeing what might be wrong in the statement to cause it to loop infinitely.
as illustrated above, you can make a recursive call in the imperative mode (and i was able to replicate this), but that’s a little odd to me that recursion like this wouldn’t also be supported in the associative mode. the associative mode seems to hold more to functional paradigms, and i guess i just think of recursion as an approach used in the functional style.
The conditional statement you’re using is just DesignScript shorthand for an If statement.
test ? returnTrue : returnFalse;
If test is true, return returnTrue, else return returnFalse. Either way, it’s probably safe to assume that any (branching) conditional statement has to be within an Imperative block.
Interesting observation, makes sense. (To provide some background, in functional programming, tail call recursion is generally used as an alternative to loops)
Maybe @Aparajit_Pratap or @Michael_Kirschner2 can throw some light on this.
@Nick_Boyts I’m afraid, an inline conditional like test ? returnTrue : returnFalse; for associative language is not entirely the same as an if-else statement. An inline conditional can be used in both Associative and Imperative code, however, in associative code, the conditional statement can replicate over its inputs, while in imperative code, the inline conditional is essentially equivalent to an if-else statement. In the following graph, you can see the difference. In the case of the associative block below, it replicates over test, while in the case of the imperative code block, test is treated as an array. Since anything other than 0 or false evaluates to true, the array evaluates to true and the imperative code executes just once (doesn’t replicate).
Ah. Thank you for clarifying, @Aparajit_Pratap. Am I correct in thinking that the associative inline statement still won’t work over an array when inside a function though?
@Nick_Boyts if you’re using the inline expression inside a function, you need to apply the replication guides on the function parameters, not in the expression, like so:
@Vikram_Subbaiah I need to come up with a good explanation of whether recursion will or will not work in the associative context and why or why not. I don’t have a good answer at the moment but will try to respond soon.
Yeah, don’t know why I didn’t do that at the time. I think I was still wrapping my head around why it was working in one case but not in another. Thank you for explaining everything. It’s been a good learning experience.
maybe i was thinking about imperative mode as too strong of a difference, like you can -only- write imperative block. I need to come back to this. Thanks for the insight!