Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Evaluating if's

 [1/15] from: chalz:earthlink at: 1-Jul-2002 23:51


Here's a little question. Perhaps I've been spoiled in other languages, but this is starting to frustrate me. I have something like: if THIS and THAT [] Thing is, if THIS is false, it continues to evaluate THAT anyways. What's the point? The result is obviously false anyways. I'm working on a case like this (perhaps someone can provide a more elegant solution): if (2 = length? p: parse filename ".") AND (not none? find pick p 2 "htm") [ ... Obviously, if the first condition is false, I want it to quit without evaluating the second condition. Help? Any way I can continue doing this in the same line, and without worrying about throwing and catching errors? Or am I more or less doomed to yet another nested if? Thanks folks. --Charles

 [2/15] from: kemp:extelligence at: 2-Jul-2002 0:55


Some languages force evaluation of the second term to ensure consistency of any side-effect behaviour. - Kemp

 [3/15] from: nitsch-lists:netcologne at: 2-Jul-2002 9:48


Am Dienstag, 2. Juli 2002 05:51 schrieb Charles:
> Here's a little question. Perhaps I've been spoiled in other languages, > but this is starting to frustrate me. I have something like:
<<quoted lines omitted: 8>>
> in the same line, and without worrying about throwing and catching errors? > Or am I more or less doomed to yet another nested if? Thanks folks.
short circuits in rebol are 'all and 'any if all[ 2 = length? p: parse filename "." not none? find pick p 2 "htm" ][ 'any is "or"
> --Charles
-Volker

 [4/15] from: atruter:hih:au at: 2-Jul-2002 14:49


> if THIS and THAT []
Try if all [a < 100 b > 10] [. . .] or if any [a < 100 b > 10] [. . .] Regards, Ashley

 [5/15] from: amicom:sonic at: 1-Jul-2002 22:12


Chalz, Instead of using AND and OR, the better REBOL way is ALL and ANY. Try this: if all [2 = length? p: parse filename "." find pick p 2 "htm"][...] I left off the "not none?" in front of the 'find because 'none automatically gets evaluated as 'false but a successful 'find (which returns an index into the series) is evaluated as 'true. You can embed ANYs and ALLs inside of each other if you have a more complex statement. I do it all the time. There are other neat uses for ANY and ALL (in particular), but I hope you get the point for this example. Later! -Bo Lechnowsky Technical Consulting REBOL VAR At 11:51 PM 7/1/02 -0400, you wrote:

 [6/15] from: al:bri:xtra at: 2-Jul-2002 20:39


Charles wrote:
> if THIS and THAT []
Try: if all [THIS THAT] [] and another to check out is: 'any. I hope that helps! Andrew Martin No nested IFs in this Rebolution! ICQ: 26227169 http://valley.150m.com/

 [7/15] from: sunandadh:aol at: 2-Jul-2002 2:38


Charles:
> Obviously, if the first condition is false, I want it to quit without > evaluating the second condition.
One way is to use the 'all function. Try these two snippets: if all [ 1 = 1 2 / 0 ] [ print "won't get here -- divide by zero error"] if all [ 1 = 99 2 / 0 ] [ print "won't get here -- first condition false"] Sunanda.

 [8/15] from: gscottjones::mchsi::com at: 2-Jul-2002 4:42


From: "Charles"
> Here's a little question. Perhaps I've been > spoiled in other languages, but this is starting
<<quoted lines omitted: 6>>
> someone can provide a more elegant solution): > if (2 = length? p: parse filename ".") AND (not none? find pick p 2
htm )
> [ ... > Obviously, if the first condition is false,
<<quoted lines omitted: 3>>
> throwing and catching errors? Or am I more or > less doomed to yet another nested if? Thanks folks.
Hi, Charles, Two helpful words are 'any and 'all. An ad hoc session at a *fresh* console demonstrates that 'all short cuts if false: if all [1 > 2 c: 3 = 3] [ print "hello"] ;== none probe c ;;;;;value of c was not set ;** Script Error: c has no value ;** Where: connect-to-link ;** Near: probe c if all [1 < 2 c: 3 = 3] [ print "hello"] ;hello probe c ;== true Hope that this both helps and causes REBOL to also spoil you. :-) --Scott Jones

 [9/15] from: brett:codeconscious at: 2-Jul-2002 17:36


Hi Charles, I believe the form, A and B is for bitwise and logical operations whereas the form and [A B] exits on the first false value it encounters in its block of conditions. So REBOL is spoiling you too :^) Your example then would look like (you don't need your parens either): if AND [ 2 = length? p: parse filename "." not none? find pick p 2 "htm" ][...] Regards, Brett. ----- Original Message ----- From: "Charles" <[chalz--earthlink--net]> To: <[rebol-list--rebol--com]> Sent: Tuesday, July 02, 2002 1:51 PM Subject: [REBOL] Evaluating if's
> Here's a little question. Perhaps I've been spoiled in other
languages, but
> this is starting to frustrate me. I have something like: > if THIS and THAT [] > Thing is, if THIS is false, it continues to evaluate THAT anyways. What's
the
> point? The result is obviously false anyways. I'm working on a case like
this
> (perhaps someone can provide a more elegant solution): > if (2 = length? p: parse filename ".") AND (not none? find pick p 2
htm )

 [10/15] from: lmecir:mbox:vol:cz at: 2-Jul-2002 9:50


Hi Charles, it is easy: if all [(2 = length? p: parse filename ".") (not none? find pick p 2 htm )] [ ... Cheers -L ----- Original Message ----- From: "Charles" Here's a little question. Perhaps I've been spoiled in other languages, but this is starting to frustrate me. I have something like: if THIS and THAT [] Thing is, if THIS is false, it continues to evaluate THAT anyways. What's the point? The result is obviously false anyways. I'm working on a case like this (perhaps someone can provide a more elegant solution): if (2 = length? p: parse filename ".") AND (not none? find pick p 2 htm ) [ ... Obviously, if the first condition is false, I want it to quit without evaluating the second condition. Help? Any way I can continue doing this in the same line, and without worrying about throwing and catching errors? Or am I more or less doomed to yet another nested if? Thanks folks. --Charles

 [11/15] from: ingo:2b1 at: 2-Jul-2002 10:00


Hi Charles, Charles wrote:
> Here's a little question. Perhaps I've been spoiled in other
languages, but
> this is starting to frustrate me. I have something like: if THIS and > THAT [] Thing is, if THIS is false, it continues to evaluate THAT > anyways. > > if (2 = length? p: parse filename ".") AND (not none? find pick p 2 "htm") [
What you are searching for is 'all, used like this: if all [ (2 = length? p: parse filename ".") (not none? find pick p 2 "htm") ] [ 'whatever-you-like-to-do-now ] 'all and its companion 'any use short-circuit evalution. Kind regards, Ingo

 [12/15] from: brett:codeconscious at: 2-Jul-2002 17:45


I should have mentioned also that ANY makes a nice "or" condition: ANY [ this or-this or-even-this or-finally-this] reduces each expression it encounters through the block not stopping until it exhausts all the expressions or it encounters one that results in a value that is not NONE and not FALSE. So ANY [1 = 3 now 3] will return the current time Brett. ----- Original Message ----- From: "Charles" <[chalz--earthlink--net]> To: <[rebol-list--rebol--com]> Sent: Tuesday, July 02, 2002 1:51 PM Subject: [REBOL] Evaluating if's
> Here's a little question. Perhaps I've been spoiled in other
languages, but
> this is starting to frustrate me. I have something like: > if THIS and THAT [] > Thing is, if THIS is false, it continues to evaluate THAT anyways. What's
the
> point? The result is obviously false anyways. I'm working on a case like
this
> (perhaps someone can provide a more elegant solution): > if (2 = length? p: parse filename ".") AND (not none? find pick p 2
htm )

 [13/15] from: g:santilli:tiscalinet:it at: 2-Jul-2002 12:21


Hi Charles, On Tuesday, July 02, 2002, 5:51:51 AM, you wrote: C> if THIS and THAT [] Use: if all [this that] [] AND is just a function like all the others. REBOL does not know in advance that it should not pass it its second argument if the first is false. ALL and ANY exist for this exact purpose. Regards, Gabriele. -- Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r

 [14/15] from: chalz:earthlink at: 2-Jul-2002 23:46


*boggles* 12 responses from 7:09am to 9:10am. Wowzers. In any event, my thanks to *everyone* who responded! It seems I should use: if all [THIS THAT THEOTHER] [..] I will keep this in mind for future reference ;) Phew! My mind still doesn't run in REBOL mode. Thanks!

 [15/15] from: al:bri:xtra at: 3-Jul-2002 16:51


> 12 responses from 7:09am to 9:10am. Wowzers.
Slow list, otherwise there would be less responses. :) Andrew Martin Coording the Rebolution with pigeons! :) ICQ: 26227169 http://valley.150m.com/

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted