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

If without a THEN

 [1/12] from: sanghabum:aol at: 9-Aug-2001 14:41


Hi all, Just purely out of curiosity, can anyone tell me why this badly-bracketed code does anything other than give a message "Script Error: if expected then-block argument of type: block"? myblock: [1 2 3 4 5 6 7 8 9 10 11 12] foreach xx myblock [ if xx = 9 remove find myblock xx ] ; for print myblock --Thanks, --Colin

 [2/12] from: ammoncooke:yah:oo at: 9-Aug-2001 13:00


ya, the code should like something like this; if xx = 9 [ remove find myblock xx ] the if statement needs to know when the *then* block (the one that contains what to do if the test statement returns true) & when it ends. HTH Ammon

 [3/12] from: christian::ensel::gmx::de at: 9-Aug-2001 21:13


Hi Colin, the answer is that your IF has got what it expected: a block - because the REMOVE statement returns a block. Insert a PROBE before IF and you'll see ... Cheers, Christian

 [4/12] from: sanghabum:aol at: 9-Aug-2001 15:30


[Ammoncooke--yahoo--com] writes:
> the code should like something like this; > > if xx = 9 [ > remove find myblock xx > ] >
Thanks Ammon, I know what's wrong with the code. What sparked my suriousity was why it runs at all--let alone the result it produces. I guess I'm just puzzled by the wierdness when I ought to be working. --Colin.

 [5/12] from: ryanc:iesco-dms at: 9-Aug-2001 12:30


Your answer is that 'remove returns a block, so 'if gets the argument type it needs. No reason for error. The contents of the block at the position returned by remove are simply evaluated, in this case to effect other than returning the last value in the block, 12. --Ryan [Sanghabum--aol--com] wrote:
> Hi all, > Just purely out of curiosity, can anyone tell me why this badly-bracketed
<<quoted lines omitted: 12>>
> [rebol-request--rebol--com] with "unsubscribe" in the > subject, without the quotes.
-- Ryan Cole Programmer Analyst www.iesco-dms.com 707-468-5400

 [6/12] from: ammoncooke:y:ahoo at: 9-Aug-2001 13:57


I recently was dealing with a lot of errors when I got confused as to the structure of if... thniking tha if needed two blocks I wrote several if statements like this: if [test block] [then block] after hours of confusion I return once again to the wonderful core.pdf... after a short time searching I discovered my error & after correcting everingthing ran beautifully! I unserstand the confusion. Enjoy!! Ammon

 [7/12] from: ryanc:iesco-dms at: 9-Aug-2001 14:09


Looked weird to me too, it never occurred to me to use 'if that way.
>> something: true
== true
>> reaction: [print "HOLY COW!"] >> if something reaction
HOLY COW!
>>
Ryan Cole wrote:
> Your answer is that 'remove returns a block, so 'if gets the argument type it > needs. No reason for error. The contents of the block at the position
<<quoted lines omitted: 30>>
> [rebol-request--rebol--com] with "unsubscribe" in the > subject, without the quotes.
-- Ryan Cole Programmer Analyst www.iesco-dms.com 707-468-5400

 [8/12] from: sanghabum:aol at: 9-Aug-2001 17:29


Thanks for all the replies. My original typo has opened up a new world of Rebol syntax for me. I can now write code like: if 1 = 1 load/all %myscript.r rather than if 1 = 1 [do %myscript.r] I'm sure I'll think of a good use for this, other than using less of the world's supply of pesky square brackets --Colin

 [9/12] from: jelinem1:nationwide at: 9-Aug-2001 16:50


By looking at the results of your original example, the unbracketted code following the 'if condition gets evaluated with every execution of the 'if word, whether or not the condition is true. Just something to be aware of. - Michael [Sanghabum--aol--com] Sent by: [rebol-bounce--rebol--com] 08/09/01 04:29 PM Please respond to rebol-list T To: [rebol-list--rebol--com] cc: bcc: Subject: [REBOL] Re: If without a THEN Thanks for all the replies. My original typo has opened up a new world of Rebol syntax for me. I can now write code like: if 1 = 1 load/all %myscript.r rather than if 1 = 1 [do %myscript.r] I'm sure I'll think of a good use for this, other than using less of the world's supply of pesky square brackets --Colin

 [10/12] from: ammoncooke::yahoo at: 9-Aug-2001 15:58


Hey, that is kewl!! Ammon

 [11/12] from: joel:neely:fedex at: 9-Aug-2001 17:31


Hi, Ryan et al., Ryan Cole wrote:
> Looked weird to me too, it never occurred to me to use 'if > that way.
<<quoted lines omitted: 4>>
> HOLY COW! > >>
An equivalent concept has been common practice in FORTH for at least 15-20 years as a way to refactor decision logic, such as moving always-the-same decisions out of a loop. As a trivial example, suppose we need to take a block of numbers and calculate either the square of each number or the square root of each number (where the decision is the same for all number in a single evaluation of the function). We could write: square-or-root: func [flag [logic!] b [block!] /local c] [ c: copy b forall c [ either flag [ change c (first c) * first c ][ change c square-root first c ] ] head c ] print mold square-or-root true [1 4 9 16] print mold square-or-root false [1 4 9 16] which behaves as follows:
>> print mold square-or-root true [1 4 9 16]
[1 16 81 256]
>> print mold square-or-root false [1 4 9 16]
[1 2 3 4] The politically-correct but trivial refactoring into square-or-root: func [flag [logic!] b [block!] /local c] [ c: copy b forall c [ change c either flag [ (first c) * first c ][ c square-root first c ] ] head c ] gives the same result, and still leaves the decision inside the loop, to be made over and over the same way each time. However, square-or-root: func [flag [logic!] b [block!] /local c work] [ c: copy b work: either flag [ [change c(first c) * first c] ][ [change c square-root first c] ] forall c work head c ] takes the decision outside the loop. Now the block in WORK is pre-configured to do The Right Thing based on the FLAG argument that doesn't change during the evaluations of the loop. HTH! -jn- This sentence contradicts itself -- no actually it doesn't. -- Doug Hofstadter joel<dot>neely<at>fedex<dot>com

 [12/12] from: john:thousand-hills at: 9-Aug-2001 18:01


If has no then or else argument. It will either operate or return "false". Try the EITHER function. EITHER argument [ DO IF TRUE ] [ DO IF FALSE ] Example: either d > 100 [ print d ] [ print d + 100 ] john else At 02:41 PM 8/9/2001 -0400, you wrote:

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