Re: Another Error
- Posted by Derek Parnell <ddparnell at bigpond.com> Jan 31, 2007
- 728 views
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. 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, 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. -- Derek Parnell Melbourne, Australia Skype name: derek.j.parnell