flowgorithm uninitialized variable - flowgorithm

I am a beginner in programming. I am making a flowgorithm for my programming class. But I keep getting an uninitialized variable. The uninitialized variable is "Speedy"
Set selectSnail = selectSnail(Speedy, Zippy, Slick, leaveRaces, snailName)
Why is it uninitialized?

A variable in Flowgorithm and many other languages has to be Declared and then Assign to it some value.
In Flowgorithm you should go to the section before calling selectSnail and declare (and assign) some value to Speedy

Related

Is there any way to define variables in classes without any default value?

I want the following behaviour:
class RealEstate:
rooms
Suddenly the following results in an Unresolved reference Error: NameError: name 'rooms' is not defined.
I am aware that I can assign None to the variable:
class RealEstate:
rooms = None
Or define the type:
class RealEstate:
rooms: float
Both will work. But it is not what I want. I want it as simple as possible and with less typing. Is there any way to make the first example work? Maybe some Metaclasses magic, some brilliant decorators, extending some special classes or libraries that can help?
No, you can't "define" a variable by writing its name alone. It will be evaluated as an expression and if it doesn't exist already it will raise a NameError.
You can either
"define" a variable by using an assignment name = value (it binds a name to a value at runtime, and you need to specify the value), or
"define" a variable by using a type annotation name: type (it associates a name with a type, for which you need to specify the type, but does not automatically create the name or any value at runtime).

Passing argument to lua function after evaluating function in string

I have functions process and matrix. The following code works
process(matrix({{2,4,6},{8,10,12},{14,16,20}}))
However the following doesn't work.
n='matrix({{2,4,6},{8,10,12},{14,16,20}})'
process(n)
It throws some error. The reason is obvious that process takes n as string rather than the output of the function matrix. So the basic difficulty involved here is about evaluating string from variable n and then give it as argument to the function process. Here loadstring function is of no use as matrix is local function and can't be referred from loadstring.
Is there any work around for this? I hope that I have clearly stated the problem here. It is about evaluating (or unloading) string and then passing it as argument to another function. Any help will be appreciated. Thanks.
as matrix is local function
Lua takes local declarations seriously. If a variable is declared local, it can only be accessed by code which is statically within the local scope of that variable. Strings which you later turn into code are not statically in the local scope and therefore cannot access local variables.
Now, with Lua 5.2+, you can provide load with a second parameter, a table which represents the global environment against which that Lua chunk will be built. If that table contains a matrix value, then the loaded string can access it. For Lua 5.1, you'd have to use setfenv on the function returned to load to accomplish a similar effect. The Lua 5.2+ method would look like this:
local env = {matrix = matrix}
local func = load("return matrix({{2,4,6},{8,10,12},{14,16,20}})", nil, "t", env)
process(func())
Note the following:
You must create an explicit table which is the global environment. There's nothing you can pass that says "make my locals available"; you have to put every local you'd like to access there. Which is, generally speaking, why you pass these things as parameters or just make them globals.
You explicitly need the "return " there if you want to get the results of the call to matrix.
You have to call the function. Functions are values in Lua, so you can freely pass them around. But if you want to pass the results of a function to another function, you have to actually call it.

Attach strings in filename

Now I have something like this
program prova
CHARACTER (LEN=4) :: mvalue
common mvalue
mvalue='01.0'
call funzione(var1, var2,...)
end
subroutine funzione()
common mvalue
*(stuff with var1, var2, ...)*
open(10,file="./prova_"//mvalue//"_.res")
end
and the compiler returns
open(10,file="./prova_"//mvalue//"_.res")
1
Error: Operands of string concatenation operator at (1) are CHARACTER(1)/INTEGER(4)
I don't know if I can use the "implicit none" instruction in the real code, because maybe it would mess up something else...I'm sorry if I can't be more precise, but as I told you I'm new to FORTRAN, and the code is kinda extended (and written EXTREMELY poorly).
I'd prefer to pass the mvalue variable to the routine, but if I try to do
program prova
CHARACTER (LEN=4) :: mvalue
mvalue="01.0"
call func(mvalue)
end
subroutine func(mvalue)
open(10,file="./prova_"//mvalue//"_.res")
end
it returns
open(10,file="./prova_"//mvalue//"_.res")
1
Error: Operands of string concatenation operator at (1) are CHARACTER(1)/INTEGER(4)
prova.f:4.16:
call func(mvalue)
Warning: Type mismatch in argument 'mvalue' at (1); passed CHARACTER(1) to INTEGER(4)
Your subroutine:
subroutine funzione()
common mvalue
*(stuff with var1, var2, ...)*
open(10,file="./prova_"//mvalue//"_.res")
end
is missing implicit none ! Always use it in every scoping unit without any exception!
The type of mvalue is not declared and it is therefore integer implicitly.
You must declare the type in every scoping unit. Sharing it using the common is not enough!
In Fortran 90 the proper thing is to use modules and not common blocks. In any programming language in general the proper way is not to share global variables (no matter if using common, using modules or any other way), but to pass the string as an argument to the subroutine.
To your new edit the same advice is still valid: Your subroutine is missing implicit none ! Always use it in every scoping unit without any exception!
If you can't use implicit none then you MUST!!!!! be prepared to diagnose errors coming from undeclared variables. It is absolutely necessary!
A character dummy argument is declared as character*(*) mvalue
As the commenters have already pointed out, you are using two different quotation markers in such a way that it passes the compile time, but fails at runtime.
The file= dummy argument expects a string, and it gets a very long one: The entirety of ./results/file_01.0_R00.res',status='unknown. But when it tries to open that file, it fails: This is simply no valid file name.
Simply replacing the ' with " in both instances (after .res and before unknown) should make the error go away.

i'm confused about languages category, can anyone please explain?

Which of the following statements is FALSE?
(A) In statically typed languages, each variable in a program has a fixed type
(B) In un-typed languages, values do not have any types
(C) In dynamically typed languages, variables have no types
(D) In all statically typed languages, each variable in a program is associated with values of only a single type during the execution of the program
Can you please explain the theory as well?
C) (In dynamically typed languages, variables have no types) Is false.
The variable has a type, however it is simply not stated or decided until run time. This implies there is no type checking prior to running the program.
a useful link describing Types and what it means:
http://en.wikipedia.org/wiki/Type_system
If you have ever done much with PHP you will notice that when you declare a varialbe, you do not have to say whether it is an INT or a STRING. However, sometimes you know that you will be receiving a string, but need an int, so you can still type cast variables at runtime, even though when you declared the variable you did not explicitly state the variable would hold an int.
<?php
#some more code here.....
# over here $myValue could be of some different type, but it can dynamically change to another type
$myValue = '5'; #storing a string...so $myValue is currently of type String
$myNewValue = (int)$myValue + 5 #type casted to integer, so in this case $myValue is currently of type integer
?>
If that doesn't help, maybe take a look at this.
myPythonVariable = "I am currently a string" #the variable is of type string
myPythonVariable = 5 #the variable is now of type integer
In the above code sample, myPythonVariable always has a type, whether or not that type changes doesn't matter.

Modifying a variable in enclosing scope

There is a common problem in languages that assume variable declarations are local. How do you get at variables in enclosing scopes.
Is there a way in Opa?
For example:
start() =
name = Random.string(5)
set_name(new_name) =
old_name = name
name = new_name
log("User {old_name} changed name to {new_name}")
This doesn't work. We get a warning that name is unused in set_name, and the value of name in start is never changed.
In a language like Lua, Javascript or Scheme, there is explicit marking of locals, so variables not marked in that way can be found in the scope stack. In Python there is no such marking, and so this would be impossible. In Python you can get at global (toplevel) variables, and I've found the #toplevel directive in Opa too. But I'm interested in intermediate points in the scope chain.
There are workarounds, of course, by using records, but is there a direct route?
One solution is to use Reference module :
Reference.create, Reference.get, Reference.set
http://opalang.org/resources/doc/index.html#stdlib.core.reference.opa.html/!/value_stdlib.core.Reference
Reference module mentioned in Fred's answer is indeed one solution to this and it's closest to what you are asking for.
But it's also good to know that when programming the "right way" most of the state in your Opa program will be captured in Sessions, with a change to the session's state triggered by a message send to it.

Resources