Re: atom() ambiguity
- Posted by "Carl R. White" <C.R.White at SCM.BRAD.AC.UK> Jun 23, 1998
- 525 views
On Mon, 22 Jun 1998, Daniel Berstein wrote: > Carl R. White wrote: > >> 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? Errm... (/me thinks hard) It does work. I'm not returning 0, I'm returning *(x = 0)*... if x = 0 then (x = not(not 0)) => (x = not(1)) => (x = 0) => True => 1 => no failure if x = 1 then (x = not(not 1)) => (x = not(0)) => (x = 1) => True => 1 => no failure The function not(not x) will always return 1 or 0, so *any other* values cause a type_check failure. > 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 If you wanted a boolean_sequence type then this is it: type boolseq(object x) if not sequence(x) return TypeFail -- Defined as 0 end if return compare(x, not(not x)) = 0 end type Combining the two types is easier and a lot simpler than it seems: type boolobj(object x) return compare({x}, not(not {x})) = 0 end type This is fun... :) -- Carl R White E-mail...: cyrek- at -bigfoot.com / Remove the hyphens before Finger...: crwhite- at -dcsun1.comp.brad.ac.uk \ mailing or fingering... Url......: http://www.bigfoot.com/~cyrek/