I've declared a boolean variable before a loop and redeclared it when the loop is complete to tell some functions inside the loop and in a following loop that need this information.
What is the correct way of doing it so that Pycharm doesnt give a warning?
before loop starts
bool_variable = False
for some variable in loop:
do something that needs to know if bool_variable is True or False
after loop finishes
bool_variable = True
Related
So here's the code I was trying to execute in Julia:
i = begin
i = 5
while(i<=10)
println(i)
i+=1
end
end
It's just basically a simple code to print the value of i from 5 to 10 but it's messing with me
Are you in the REPL? What you are probably running into is that begin does not introduce its own scope, so i = 5 declares i as a global variable. Because while does introduce its own scope, if you do println(i), it only looks for i in its local scope where it is not defined, because i exists only as a global variable. You can add a line global i at the beginning of the body of the while loop to tell all code after that to use the global i, but note that global variables come with their own performance caveats. An arguably better solution would be to use let instead of begin, which does introduce a new scope, but note that then you can of course not access i afterwards, because it is now only local to the let block.
This behavior will actually be changed in the upcoming release of Julia 1.5, so your code should then just work.
Your issue is scope. When you enter into a loop, variables created inside the loop are local to the loop and destroyed after it exits. i is not currently defined inside your while loop, so you get the error. The quick fix is to tell Julia you want the loop to have access to the global i variable you defined at the top by adding global i immediately after the while statement. You also don't need the begin block, and naming the block i is immediately overwritten by the next statement defining i.
I am used to implement a recursive function that checks if a given list L is written in a reverse-order:
orderIsReverse(L):-
[X|Q]=L,
[XP|_]=Q,
(X<XP -> false; orderIsReverse(Q)),
true.
However after compiling the code and prompting orderIsReverse([3,2,1]) within SWI Prolog, I get false returned.
What's wrong with the code?
You need to handle the case when the input list is empty (and also when it contains one single element, as you need two for a comparison).
orderIsReverse([X1,X2|L]):-
X1 > X2, orderIsReverse([X2|L]).
orderIsReverse([_]).
orderIsReverse([]).
Update: fixed the logic.
This particular example function is in Lua, but I think the main concepts are true for any language with lexical scoping, first-class functions, and iterators.
Code Description (TL;DR -- see code):
The code below defines an iterator constructor which merely defines a local value and returns an iterator function.
This iterator function, when run, uses the local value from the constructor and increases that value by 1. It then runs itself recursively until the value reaches 5, then adds 1 to the value and returns the number 5. If it's run again, it will run itself recursively until the value reaches 20 or higher, then it returns nil, which is the signal for the loop to stop.
I then run an outer loop using the iterator provided by the constructor, which will run the body of the loop when the iterator returns a value (5). In the body of the outer loop I put an inner loop that also uses an iterator provided by the constructor.
The program should run the body of the loop once (when the value in the outer loop iterator hits 5) and then run the inner loop completely (letting the inner loop value hit 20), then return back to the outer loop and run it until it finishes.
function iterConstr()
local indexes = {0}
function iter()
print(indexes[1])
indexes[1] = indexes[1] + 1
if indexes[1] == 5 then
indexes[1] = indexes[1] + 1
return 5
elseif indexes[1]>=21 then
print("finished!")
return nil
else
print("returning next one")
return iter()
end
end
return iter
end
for val in iterConstr() do
for newVal in iterConstr() do
end
print("big switch")
end
The behavior I did not expect is that the value from the inner loop and outer loop seem to be linked together. When focus is returned to the outer loop after running through the inner loop, it runs with the expected next value for the outer loop (6), but then instead of iterating and incrementing up to 20, it instead jumps immediately to 21, which is where the inner loop ended!
Can anyone help explain why this bizarre behavior occurs?
I believe your problem lies on line 3 - you declare iter as a global function instead of a local one, which makes it accessible from any Lua chunk. Changing that line to local function iter() corrects this behaviour. I think therefore that this is more of an issue with the developer applying lexical scoping than with lexical scoping itself :)
Suppose I have a small function in R
getsum <- function(a, b){
c <- a+b
}
Now when I run this function, It runs rormally. But my question is can I check the assigned value to c on console? I know that:
I can print its value within function, which will get reflected on console
I can return this value via return keyword.
I don't want any of these. My question is specifically, whether I can check the value of variables inside a function at console. I tried functionname::variablename, but it is not working
So there are a couple of answers I could give here, I'm not positive what you need it for.
(EDIT: oh, and FYI, I would avoid using "c" as a variable name, that's a function, the ambiguous value is bad. R will compensate by using the value in your current environment, but that's a stopgap measure)
The easiest would be to assign some variable:
x <- NULL
at the global environment level, and then, somewhere, during the function:
x <<- c
the double arrow assigns the value to the topmost environment.
However, this is a bit dangerous, as it's going to run as far up as it can to assign that variable. If you're after debugging, then I recommend adding:
browser()
inside the code somewhere, it will stop execution during the function and then you can run whatever you like inside the function environment.
If you want to debug your code, just use RSTUDIO https://www.rstudio.com/ is an IDE with debugging capabilities. In Rstudio setting a breakpoint inside your function and can check the value of internal variables in the right panels.
I wrote a function that outputs a single element numeric after a 300-cycle for loop. I make it print about 10 lines in each cycle, to know where it's at. Now I want to run this for loop itself in a 1000 cycle for loop (and place the resulting numbers in a matrix). But it prints way to much stuff and I don't know where it's at in the execution of the outer (1000 cycle) for loop. The output from the inner for loop overwhelms a print statement executed at each of the outer loop's cycles. Here's how it looks:
for(i in 1:1000){
function(...){...} #prints 10 lines 300 times before outputting a single element numeric
cat("Outer loop step "); print(i)}
Now I don't want to remove the print statements from my function, but I want to mute them when I call the function in that for loop. How can I run my function without executing it's print() statements?
Modify your function so you can pass in a "debug" true/false parameter to control the print statements.
Don't use print or cat. Use message instead. You can then use suppressMessages to suppress the message output.