Re: Another Error
- Posted by Robert Craig <rds at RapidEuphoria.com> Feb 01, 2007
- 653 views
Derek Parnell wrote: > Rich Klender wrote: > > Well, I hit another snag, if anyone would be gracious enough to help!!! > > > > Here's the code: > > I've reduced it to the problem area ... > > > sequence Correct > > if Correct = 'y' or Correct = 'Y' then > > > true/false condition must be an ATOM > > This has been one of my few disappointments with Euphoria. Robert Craig, the > creator of the language, has defined the 'if' statement in a manner which most > other languages do not. Which languages are you including in your list of "most other languages"? I know Basic works the way you would like, but C/C++ (a more popular language) does not. People who come to Euphoria from a Basic background are often confused by this, but many other people, such as myself, who are used to C/C++, think it's fine to require a function call to compare strings or arrays in this way. This is not something that Robert Craig invented, all on his own, just to annoy the rest of the world. > Bear with me please, as I understand that this is your > first real programming effort - I may get a little technical here > > Euphoria defines the IF statement as > > IF :: 'if' ATOMEXPRESSION 'then' > [ STATEMENTBLOCK ] > [ ELSEPHRASE ] > 'end if' > > but most other languages define it as > > IF :: 'if' COMPARISIONEXPRESSION 'then' > [ STATEMENTBLOCK ] > [ ELSEPHRASE ] > 'end if' > > The difference is that Euphoria evaluates the expression that follows the 'if' > keyword in a manner that is not expected by nearly everyone else except > Robert. > Taking your example code, this is what happens... > > if Correct = 'y' or Correct = 'Y' then > > Let's assume that the user entered "Y" into the variable Correct. Euphoria > will > first evaluate > > "Y" = 'y' > > which is a sequence operation, meaning for each element in the sequence it > will > apply the " = 'y' " fragment. Remember that strings are sequences so the > value > in Correct is equivalent to > > {'Y'} > > so the result of this first sequence operation is > {'Y'} = 'y' ===> {0} > because the operation compares each element in Correct with 'y' and replaces > the result with either 1 or 0 depending on the comparision result. 1 means > true > and 0 means false. > > The second sequence operation is > > Correct = 'Y' > > which is the same as > > {'Y'} = 'Y' > > which results in {1} > > The final sequence operation is then > {0} or {1} > which results in {1} > > The net result of this is that your IF statement is equivalent to > > if {1} then > > but this is wrong as Euphoria requires the expression to result in an atom > value > and not a sequence. This is why Euphoria doesn't like things like > > if "yes" then > > But back to most other languages... > > if Correct = 'y' or Correct = 'Y' then > > they would evaluate the expression as a comparision instead. > > Correct = 'y' ===> false > Correct = 'Y' ===> true > false or true ===> true > > and thus your statement is equivalent to > > if true then > > which makes a lot more common sense than Robert's concept of an IF statement. > > > However, as Robert's philosophy on this is never going to change, Yeah, I guess I'm just a stubborn old fool. > the only safe > way to code equality tests in IF statements in Euphoria is this ... > > if equal(Correct, 'y') or equal(Correct, 'Y') then > > which is not an intuitive alternative. I assume you meant: if equal(Correct, "y") or equal(Correct, "Y") then Anyway, thanks for the tutorial. I know there are newbies who are confused by this feature. For what it's worth, I'd probably code it as: if find('Y', upper(Correct)) then ... which will allow the user to type any of: Y y yes Yes YES yeah etc. rather than insisting that he type precisely "y" or "Y" (with no extra characters, leading or trailing blanks etc.). Or perhaps something like: if find(upper(Correct), {"Y", "YES", "OK", "OUI", "JA", "SI"}) then Regards, Rob Craig Rapid Deployment Software http://www.RapidEuphoria.com