Forth language EBNF rule for the while loop [duplicate] - ebnf

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Forth language EBNF rule for an infinite loop or if statement
Is there any EBNF rule that describes a Forth while loop (or any other loop)?

BEGIN <code> WHILE <code> REPEAT — Executes the first <code> block and consumes whatever it leaves on top of the stack; if it's nonzero, the second <code> block is executed, and then everything starts over again. If it's zero, execution skips to after the REPEAT. Note that this can only be used inside a definition (e.g., ": foo ... ;").

Related

Why choosing `unwrap_or_else` over `unwrap_or`? [duplicate]

This question already has answers here:
Why should I prefer `Option::ok_or_else` instead of `Option::ok_or`?
(3 answers)
Closed 3 years ago.
fn main() {
let _one = None.unwrap_or("one".to_string());
let _two = None.unwrap_or_else(|| "two".to_string());
}
Any particular reason why people should prefer unwrap_or_else over unwrap_or?
I have seen comments that unwrap_or is eager (this as an example). Does that mean the values in unwrap_or are always evaluated before program execution? And the FnOnce values in unwrap_or_else is called only when the program execute up to that line?
Both are evaluated during the program's execution and can be arbitrary values. The difference is that:
With unwrap_or the fallback value is evaluated just before unwrap_or is called, and therefore is evaluated whether it's needed or not (because Rust is an eager language).
With unwrap_or_else the fallback value is evaluated only when unwrap_or_else triggers it (by invoking the function you pass), and therefore is evaluated only if it's needed.

Ruby lexical scope inside iterator block [duplicate]

This question already has answers here:
Why can I refer to a variable outside of an if/unless/case statement that never ran?
(3 answers)
Closed 5 years ago.
In Ruby (v2.5.0)...
[1,2,3].map do |i|
if i.eql?(3)
a = 123
end
defined?(a)
end
=> ["local-variable", "local-variable", "local-variable"]
Can someone please explain to me how a can be a local-variable (equal to nil) in the first and second iteration, if it's not set until the third iteration?
Thanks in advance!
I will answer quoting a book by A.Black: Well Grounded Rubyist, Chapter 6, p. 158. (second edition 2014):
When the Ruby parser sees the sequence identifier, equal-sign, and value, as in this expression,
a = 123
it allocates space for a local variable a. The creation of the variable - not the assignment of a value to it, but the internal creation of a variable - always takes place as a result of this kind of expression, event if the code isn't executed.

What's the purpose of `do` in the while loop? [duplicate]

This question already has answers here:
Ruby while syntax
(2 answers)
Closed 7 years ago.
A while loop works with or without do. Please explain why the following two snippets works the same way.
Without do
i = 1
while i < 5
i = i + 1
end
With do
i = 1
while i < 5 do
i = i + 1
end
The non-theoretical answer is quite simply: because the Ruby Syntax Guide said so.
The language syntax guide defines the do keyword as optional for both while and until loops.
From what I understand - and it is mostly a theory - allowing the do to be optional rather than required is mostly for compatibility and allowing for harmonic syntax within the language.
I think of it as an acknowledgment for the "gray" areas, where things are less absolute. The same is done in physics, where light behaves both as a particle and as a wave and we should acknowledge both aspects.
Coming from different languages and school of thought, while is somewhere between a pure keyword (like if) and a method (like loop, which is defined under the Kernel module)
if statements do not require a do to begin a block of code and under the same logic, while loops wouldn't require a do keyword.
This is while as a keyword.
On the other hand, loop requires the do keyword (or the {block}), so why shouldn't while have the same semantics?
This is while as a method (as far as syntax goes).
Ruby is about making the programmer happy. Allowing the do to be optional makes all programmers happy and doesn't require that the Ruby programmer resign themselves to just one school of thought related to the nature of while.
x = 0
while x<3 do puts "hello"; x+=1 end
--output:--
hello
hello
hello
The do is optional, and can be used to clarify a code as in the answer to be on one line. Separation can be done with a do, newline, \, or semicolon.

What is the name of the part of the parenthesized expression of a for-loop that terminates the loop? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to answer the following question:
What is the name of the part of a for-loop that terminates the loop?
The parenthesis is the (), but I don't get the rest of the question. If I had a loop like:
// enter code here
int k = 0;
for (int j=0;j<=10;j++)
{
k++
}
what is the name of the j<=10 sub-expression?
What is the name of the part of the parenthesis...of a for-loop that terminates the loop?
In your example, j<=10 is the part of the overall loop expression that defines the exit condition for the loop. Some sources describe this part of the loop expression as the continue or end condition. This sub-expression can probably be described in other ways, too, but exit condition is pretty unambiguous and clearly communicates its purpose and intent. Whenever the exit condition evaluates as true, the loop will exit.
You can, of course, also place additional loop controls inside the body of the loop such as break or continue. However, these flow-control keywords aren't part of the definition of the loop itself, and are thus outside the scope of your original question.
You might call it the for loop's "condition" as it's called at https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/for for example.
for ([initialization]; [condition]; [final-expression])
statement
The answer is "the break statement".

What is the ruby Regex or String #=== method/operator? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
=== vs. == in Ruby
I can't find verbose docs on this at all. The doc page is broken:
http://ruby-doc.org/core-1.9.3/String.html
The regex page uses the word "case" in two different senses (!) and I can't understand what the point is:
http://www.ruby-doc.org/core-1.9.3/Regexp.html#method-i-3D-3D-3D
And it was in use in Rails:
https://github.com/rails/rails/commit/3756a3fdfe8d339a53bf347487342f93fd9e1edb?utm_source=rubyweekly&utm_medium=email
=== is the "case equality" operator:
In Ruby, triple equals (Object#===) is, "effectively the same as calling #==, but typically overridden by descendants to provide meaningful semantics in case statements".
See http://andy-payne.com/2008/09/confusion-over-triple-equals/

Resources