Home   Archive   Permalink



Vernacular condition

I do not yet speak the REBOL as she should be spoken. I am wondering about the 'vernacular' way to express a condition, where I have some code (for example) and I want to find out if it is (for example) a valid value of A or B or C or D.    
    
In COBOL I can say:
     IF CODE = 'A' OR 'B' OR 'C' OR 'D'
    
In SQL I can say:
     CODE in ('A', 'B', 'C', 'D')
    
In REBOL, is there a better way than this (which does work just fine):
     either (equal? CODE 'A') or
             (equal? CODE 'B') or
             (equal? CODE 'C') or
             (equal? CODE 'D') [
         print 'valid'
     ] [
         print 'invalid'
    
Thank you.


posted by:   Steven White       25-Oct-2016/12:34:34-7:00



One way:
    
print either find ["A" "B" "C" "D"] CODE ["Valid"]["Invalid"]

posted by:   Ennis       25-Oct-2016/13:01:30-7:00



In Rebol the 'ALL and 'ANY functions replace the AND and OR in other languages:
print either any [code = "A" code = "B"
                    code = "C" code = "D"][
         "valid"
][
         "invalid"
]
    
Although Ennis' example is a very nice one, it is specific for string! (series!) type comparison.

posted by:   iArnold       25-Oct-2016/13:44:59-7:00



iArnold: I wouldn't say that's entirely the caseó
    
code: 1.7
either find [1 foo@bar 1.7 word] code ["valid"]["invalid"]
    
There is also SWITCH:
    
switch/default code [
     "A" "B" "C" "D" ["valid"]
]["invalid"]

posted by:   Chris       25-Oct-2016/20:39:34-7:00



Chris, you are correct.
But when using Red there is another catch with a multi selection in a switch. It is (used to be?) not supported when compiled and can lead to unexpected results.


posted by:   iArnold       31-Oct-2016/16:41:51-7:00



You asked:
    
"I want to find out if some code is a valid value of A or B or C or D."
    
Rebol deals with data, specifically recognized data that can be loaded into REBOL and "assigned" a datatype!. The REBOL exec evaluates to produce fully reduced.
    
;; assuming
code: 'a
a: 'a
b: 'b
c: 'c
d: 'd
    
;; like COBOL
>> if (code = a) or (code = b) or (code = c) or (code = d) [print "yes"]
yes
    
;; REBOL
>> if any [equal? code a equal? Code b equal? Code c equal? Code d][print "yes"]
yes
    
;; Ennis' REBOL way
>> if found? find [a b c d] CODE [print "yes"]
yes
    
;; Pattern-based Decision-Expression
>> decisions: [
[     ['a | 'b | 'c | 'd] (print "yes")
[    ]
== [
     ['a | 'b | 'c | 'd] (print "yes")
]
>> parse to-block code decisions
yes
== true
>> code: 'z
== z
>> parse to-block code decisions
== false
    
ALSO ...
    
#"A" for char!
"A" for string!
    
You cannot do 'A' in REBOL. 'A' means this:
    
>> 'A'
== A'
    
It's a literal capital A with a tick.
    


posted by:   Time Series Lord       2-Nov-2016/18:39:15-7:00



Arnold--did not know that. SWITCH has always been a strange beast.

posted by:   Chris       3-Nov-2016/18:24:41-7:00