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

Multiple Word Conditional?

 [1/8] from: rsnell::webtrends::com at: 16-Nov-2000 9:33

Given three mutually exclusive refinements in a function (or just three words in a script), what is the best way to perform the conditional? one: false two: false three: true Of course we can do: either one [print "1"] [either two [print "2"][print "3"]] but this gets ugly for more than three words and I thought that someone recently posted a trick using 'any that made this simpler and more readable. Can't find it though. Any suggestions? Thanks, Rodney

 [2/8] from: larry:ecotope at: 16-Nov-2000 9:59

Hi Rodney One nice way to do this is:
>> one: false
== false
>> two: false
== false
>> three: true
== true
>> >> any [all [one print "1"] all [two print "2"] all [three print "3"]]
3 Notice that ANY and ALL can return the values of any expression as well as just true and false. BTW, the construct above has the same functionality as a COND in Scheme or Lisp. HTH -Larry

 [3/8] from: joel:neely:fedex at: 16-Nov-2000 12:57

Hi, Rodney, I believe it was Gabriele... (Credit where credit is due, and all that...) [rebol-bounce--rebol--com] wrote:
> Given three mutually exclusive refinements in a function > (or just three words in a script), what is the best way
<<quoted lines omitted: 9>>
> though. Any suggestions? >> one: two: not three: true == false
any [ if one [print "1"] if two [print "2"] if three [print "3"] ] 3
-jn- -- ; Joel Neely [joel--neely--fedex--com] 901-263-4460 38017/HKA/9677 REBOL [] foreach [order string] sort/skip reduce [ true "!" false head reverse "rekcah" none "REBOL " prin "Just " "another " ] 2 [prin string] print ""

 [4/8] from: rsnell:webtrends at: 16-Nov-2000 11:30

Thanks a lot Joel, Larry (and Gabriele!). Excuse me for being obtuse here but I'm having a little trouble understanding it what is going on. The help for 'any says "evaluates and returns the first value that is not false or none". But this means that the if statements (if one [print "1"]) must evaluate and become a value. However if I try to obtain that value: v: if one [print "1"] I get a script error indicating that the (if...) does not return a value. How does 'any know that one of the (if...)'s were taken if they don't return a value? TIA, Rodney

 [5/8] from: rebol:techscribe at: 16-Nov-2000 15:16

Hi Rodney if returns none if the condition is false, otherwise if's body is evaluated and the result of evaluatiing the final expression in this block is returned (if any). I,.e
>> if false [ "ok" ]
== none
>> if true ["ok"]
== "ok"
>> if true [] >>
In the last scenario the block did not return a value, and therefore there was no return value for this expression. In summary, an if expression is only guaranteed to return a value if it fails, namely the value none. If the condition evaluates to true, then the result is determined by the expression contained in the block. The word any evaluates the sequentially evalutes the expressions contained in its block as long as the the expressions evaluate to false logic value. (or the block is exhausted). Caution: If I generalize your example
>either one [print "1"] [either two [print "2"][print "3"]]
then you want the expressions associated with two (or three) to be evaluated only if the condition for the first expression fails. But
>> amy [ if condition-1 [expression-1] if condition-2 [expression-2 ]
] is not identical to
>> either condition-1 [expression-1] [ if condition-2 [expression-2] ]
In the case of any, if expression -1 evaluates to a logical false value then condition-2 will be tested, even though condition-1 evaluated to true. Using either, however, expression-2 will only be evaluated if condition-1 failed and condition-2 succeeds.
>> if true [false]
== false
>> any [ if true [false] if true [print "second expression"]
== second expression. In the either case the "second expression" string will not be printed.
>> either true [false] [print "second expression"]
== false In short any: expression-2 will be evaluated in two cases: (1) condition-1 fails and condition-2 succeeds. (2) condition-1 succeeds, expression-1 returns a logical false value, and condition-2 succeeds. either: expression-2 will be evaluated only in case (1) condition-1 fails and condition-2 succeeds. There is no second case here. That's a subtle difference that may cause some headscratching, if you don't account for it in your algorithm. Hope this helps, Elan

 [6/8] from: chaz:innocent at: 19-Nov-2000 0:24

any is pretty cool! That does help! factorial: func [num][ fact: func [n a][ any [ all [lesser? n 0 print "I don't know"] all [switch n [0 1 a]] all [fact subtract n 1 multiply n a] ] ] fact num 1 ] chaz At 09:59 AM 11/16/00 -0800, you wrote:

 [7/8] from: al:bri:xtra at: 19-Nov-2000 21:53

Chaz wrote:
> factorial: func [num][ > fact: func [n a][
<<quoted lines omitted: 6>>
> fact num 1 > ]
This can be better written as: Factorial!: make object! [ Fact: func [n a] [ any [ all [lesser? n 0 print "I don't know"] all [switch n [0 1 a]] all [fact subtract n 1 multiply n a] ] ] set 'Factorial func [num][ Fact Num 1 ] ] Note that Fact is only defined once, in the 'Factorial! object, rather than every time 'Factorial is executed. I hope that helps! Andrew Martin Objective Rebol... ICQ: 26227169

 [8/8] from: g:santilli:tiscalinet:it at: 17-Nov-2000 11:38

Rodney Snell wrote:
> Of course we can do: > > either one [print "1"] [either two [print "2"][print "3"]]
print any [ if one ["1"] if two ["2"] if three ["3"] "4" ; default ] Keep in mind that ANY returns the first value found that is different from NONE or FALSE; IF returns NONE if the condition isn't met, else returns the result of evaluating the body block. HTH, Gabriele. -- Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer Amiga Group Italia sez. L'Aquila --

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