Computer Science Canada Changing Variables |
Author: | Evdawg [ Sun Mar 26, 2006 11:05 am ] |
Post subject: | Changing Variables |
Hi, I have declared an integer variable with the var command. Is there any way to change this variable later in the program, or is there an equivalent command that I can use that is able to overwrite itself? What I am trying to do is use variables to store x and y values for "for" loops in an animation (e.g. for x:var1..var2). Instead of repeating the code, I have put the code into a process. Now what I want to do is be able to redeclare those variables whenever I "fork" that process. I hope that was clear enough. I'm really confused overall about Turing (my teacher isn't so good). Thanks in advance! |
Author: | MysticVegeta [ Sun Mar 26, 2006 11:15 am ] |
Post subject: | |
I am sure there is another way of doing it using loops ![]() ![]() |
Author: | Evdawg [ Sun Mar 26, 2006 11:33 am ] | ||
Post subject: | |||
Here's a chunk of the code:
Thanks for your responce, MysticVegeta. A chunk of my code is above, and it (obviously) does not run because I cannot redeclare the variables like that. So what can I do to maintain the same functionality? And also, what would I use instead of a process? |
Author: | Delos [ Sun Mar 26, 2006 1:25 pm ] | ||
Post subject: | |||
You cannot redeclare variables, but you can reassign them.
As for alternates to Processes, take a stroll along the [Turing Walkthrough] and look out for the signs pointing to Procedures, Functions, and Processes. The former two will give you some interesting insights into what you can do while avoiding the evil processes. (Processes in general are not evil, just that Turing handles them badly, and it takes a lot of work and nifty control to get them to do what you want them to.) |
Author: | Evdawg [ Sun Mar 26, 2006 1:56 pm ] |
Post subject: | |
That looks like exactly what I needed. Delos, thanks so much! ![]() |
Author: | Cervantes [ Sun Mar 26, 2006 2:55 pm ] |
Post subject: | |
Indeed, Processes are evil. Might we get that autolinking? |
Author: | MysticVegeta [ Sun Mar 26, 2006 4:44 pm ] |
Post subject: | |
Yes that the exactly the reason why they are called variables because their value could vary. Otherwise they would just be called constants. Also look at your for loops. suppose there is a loop "for x : 1..n" The x serves as a variable because its value changes from 1 to n, but you cannot edit it and say that x := 10, because it has restrictions. |
Author: | chrispminis [ Sun Mar 26, 2006 6:47 pm ] | ||||
Post subject: | |||||
Hmmm, just on the subject of redeclaring here's a little question. If you are in a situation when you declare an array with the upperbound being the value of another variable, so that you could change it easily if multiple arrays used that value. ex.
or
Say you wanted to loop your program and add in a restart function that returned you to the beginning. If the length of chrisRules changed is it possible to redeclare the array (or reassign) so that chrisRulesALot has updated bounds? |
Author: | MysticVegeta [ Sun Mar 26, 2006 7:26 pm ] | ||||
Post subject: | |||||
Ah, reminds me of Cervantes again, His Flexible Arrays!!! Look for the tuts, let me give a quick walkthrough:
How the hell do we change it now?
|
Author: | Cervantes [ Sun Mar 26, 2006 8:13 pm ] |
Post subject: | |
chrispminis wrote: Say you wanted to loop your program and add in a restart function that returned you to the beginning. If the length of chrisRules changed is it possible to redeclare the array (or reassign) so that chrisRulesALot has updated bounds? If the array is local (as in, local variables - declared within the loop), then sure. The array's upper bounds will change each time through the loop. (Untested.) However, if the array is global (declared outside the loop), then you have to use a flexible array or some other data structure for this. |
Author: | MysticVegeta [ Mon Mar 27, 2006 1:33 am ] | ||
Post subject: | |||
Yup, Geoff is right (not surprised) Here is the tested version:
|
Author: | Martin [ Mon Mar 27, 2006 3:02 am ] | ||||
Post subject: | |||||
You can pass parameters to functions and processes in turing. instead of doing this:
do this
|
Author: | MysticVegeta [ Mon Mar 27, 2006 11:17 am ] |
Post subject: | |
Yes but then you will have to call the procedure again to change the values of the variables, and if you want to change those values inside from those procedures, then you have to recurse it, even though if your problem can be solved without recursions |
Author: | Delos [ Mon Mar 27, 2006 3:16 pm ] |
Post subject: | |
And recursion is a bad thing? Well, if it spirals out of control yes, but in all generality, recursion will usually optimize your coding experience. Anyhow, parameters are like cheesecake - you just can't go wrong with them. Unless they're badly made, then you'll just get a stomach ache. |
Author: | MysticVegeta [ Mon Mar 27, 2006 6:03 pm ] |
Post subject: | |
no well, I wasnt saying that its a bad thing, I was just saying if the problem can be solved with using simple arrays then why use recursions? They could get slow and you would have to run your entire procedure just to change the values of a few variables? |
Author: | Cervantes [ Mon Mar 27, 2006 9:26 pm ] |
Post subject: | |
I can see why you think that. I did. But really, learn O'Caml. You won't think about functions the same way ever again. ![]() |
Author: | Delos [ Mon Mar 27, 2006 9:44 pm ] |
Post subject: | |
Better cite him just to be sure. |