Re: atom() ambiguity
- Posted by Daniel Berstein <daber at PAIR.COM> Jun 22, 1998
- 669 views
-----Original Message----- De: Carl R. White <C.R.White at SCM.BRAD.AC.UK> Para: EUPHORIA at cwisserver1.mcs.muohio.edu <EUPHORIA at cwisserver1.mcs.muohio.edu> Fecha: lunes 22 de junio de 1998 12:26 Asunto: Re: atom() ambiguity >> type boolean (integer x) >> if x then -- If 'x' is != 0 >> return 1 >> else >> return 0 -- If x = 0 >> end type > >This code won't work. A program would crash if it a boolean was set to >false(0) and wouldn't crash if x = 7 (for instance). Quite right Carl... Mea Culpa ;) >After thinking a lot recently, this is the best solution (IMHO): > >constant TypeFail = 0 >constant True = 1, False = 0 > >type boolean(object x) > if not integer(x) then > return TypeFail > end if > return x=not(not x) >end type Does it work? If x=False (0) then: return x=not(not x) return x=not(1) return x=0 --->False Isn't this the same I posted before, giving a type_check error? The type declaration should be: type boolean(object x) integer t if sequence(x) then if length(x) = 0 then t = 0 else for i=1 to length(x)-1 do t = boolean(x[i]) and boolean(x[i+1]) end for end if else t = 1 end if return t end type This type declaration says an object is a valid boolean variable if and only if: 1.- It's an atom (can be true or false). 2.- It's a tautological (all true: != 0)or completly negational (all false: = 0) sequence. Regards, Daniel Berstein daber at pair.com