while statement problem
[1/8] from: capolunghi::att::com at: 3-Oct-2000 9:57
Anyone,
Just learning REbol and need some explanation as to how to do this:
while [ prime and test <= square-root number ] [..]
Rebol complains that i can't use an and . Can multiple conditions be placed
in the while clause ??? If so, how do you do it?
Thanks in advance!
Joe
[2/8] from: petr:krenzelok:trz:cz at: 3-Oct-2000 16:19
[capolunghi--att--com] wrote:
> Anyone,
>
> Just learning REbol and need some explanation as to how to do this:
>
> while [ prime and test <= square-root number ] [..]
look at 'all and 'any functions ....
while [ all [(prime <= square-root number) (test <= square-root number)] ....
parens () nod needed ...
not sure if it's what you wanted to achieve ...
btw: and should work imho too, just use parens ...
while [(prime <= square-root number) and (test <= square-root number)] ....
Cheers,
-pekr-
[3/8] from: capolunghi:att at: 3-Oct-2000 10:56
Petr:
Thanks. Your solution led me to conclude that you can't do this:
prime : true
while [ prime and ... ] [ .. ]
Seems rebol does not interpret 'prime as true in this case. The workaround
is to state:
while [ (prime = true) and (...) ] [ .. ]
You have to explicitly state the conditions in the while clause. Strange ..
Joe
[4/8] from: petr:krenzelok:trz:cz at: 3-Oct-2000 17:39
[capolunghi--att--com] wrote:
> Petr:
>
> Thanks. Your solution led me to conclude that you can't do this:
>
> prime : true
^^^
prime: true ; note the colon ... a typo on your side?
while [prime and (1 = 1)][prin "."]
..............................................................................................................................
..............................................................................................................................
...................................... etc.
works, or not?
Cheers,
-pekr-
[5/8] from: joel:neely:fedex at: 3-Oct-2000 11:06
Hello, Joe!
[capolunghi--att--com] wrote:
> Petr:
> Thanks. Your solution led me to conclude that you can't do this:
<<quoted lines omitted: 4>>
> while [ (prime = true) and (...) ] [ .. ]
> You have to explicitly state the conditions in the while clause. Strange ..
Not at all! (Pardon the pun! ;-)
All of these work:
>> use [nonseven counter total] [
[ nonseven: 7 <> counter: total: 0
[ while [all [nonseven total < 50]] [
[ total: total + counter
[ nonseven: 7 <> counter: counter + 1
[ ]
[ print [nonseven counter total]
[ ]
false 7 21
(bailing out when nonseven becomes false)
>> use [nonseven counter total] [
[ nonseven: 7 <> counter: total: 0
[ while [all [nonseven total < 10]] [
[ total: total + counter
[ nonseven: 7 <> counter: counter + 1
[ ]
[ print [nonseven counter total]
[ ]
true 5 10
(or bailing out when total hits the stated limit)
>> use [nonseven counter total] [
[ nonseven: 7 <> counter: total: 0
[ while [nonseven and (total < 10)] [
[ total: total + counter
[ nonseven: 7 <> counter: counter + 1
[ ]
[ print [nonseven counter total]
[ ]
true 5 10
(or doing this with and instead of all in the control expression)
I think Petr read your code:
> >
> > while [ prime and test <= square-root number ] [..]
> >
as intending to control on "while prime and test are both at most
square-root of number" which is what his reply actually does:
> while [ all [(prime <= square-root number) (test <= square-root number)]
> ....
> parens () nod needed ...
>
> not sure if it's what you wanted to achieve ...
>
> btw: and should work imho too, just use parens ...
>
> while [(prime <= square-root number) and (test <= square-root number)] ....
>
The REAL root cause of your error message is that REBOL doesn't use
operator precendence (as other programming languages do). Consecutive
operators are evaluated left-to-right, meaning that your original:
prime and test <= square-root number
is evaluated with the same meaning as
(prime and test) <= square-root number
which would try to apply and to a boolean and a number (and then would
try to apply <= between a boolean and a number, but we don't get that far).
The first of these generates the show-stopping error, as in:
>> use [nonseven counter total] [
[ nonseven: 7 <> counter: total: 0
[ while [nonseven and total < 10] [
[ total: total + counter
[ nonseven: 7 <> counter: counter + 1
[ ]
[ print [nonseven counter total]
[ ]
** Script Error: Expected one of: logic! - not: integer!.
** Where: nonseven and total < 10
Hope this helps!
-jn-
--
; Joel Neely [joel--neely--fedex--com] 901-263-4460 38017/HKA/9677
REBOL [] print to-string debase decompress #{
789C0BCE0BAB4A7176CA48CAB53448740FABF474F3720BCC
B6F4F574CFC888342AC949CE74B50500E1710C0C24000000}
[6/8] from: capolunghi:att at: 3-Oct-2000 12:49
Petr:
Yes it worked. Sorry for the typo. Thanks for your help.
Joe
[7/8] from: capolunghi:att at: 3-Oct-2000 12:55
Thanks Joel!
I appreciate your take on this.
Joe
[8/8] from: rebol:techscribe at: 3-Oct-2000 11:17
[joel--neely--fedex--com] wrote:
> The REAL root cause of your error message is that REBOL doesn't use
> operator precendence (as other programming languages do). Consecutive
<<quoted lines omitted: 5>>
> try to apply <= between a boolean and a number, but we don't get that far).
> The first of these generates the show-stopping error, as in:
Correct.
BTW, REBOL does accept boolean operations on integers, tuples and issues.
(perhaps other datatypes?) But all values must be consistent and share the same
datatype. For instance:
>> 1 and 1
== 1
>> 1 and 0
== 0
But
>> 1 and true
** Script Error: Expected one of: integer! - not logic!.
** Where: 1 and true
Elan
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted