Re: Minimum & Maximum
- Posted by "Carl R. White" <C.R.White at SCM.BRAD.AC.UK> Sep 25, 1998
- 518 views
On Fri, 25 Sep 1998, jiri babor wrote: > I think you have to include <= and >= comparisons respectively in your > functions, as it is done in Mike's. Your functions return zeros for equal > elements. jiri I make a mistake every now and again (coding on the fly and all that) :) Your recursive solution seems to handle *some* type mismatches, but not all. What if length(a) != length(b)? > function minimum(object a, object b) > if atom(a) then > if b<a then a=b end if > else > for i=1 to length(a) do > a[i]=minimum(a[i],b[i]) > end for > end if > return a > end function how about:? function typeof(object x) if sequence(x) then return length(x) else return 0 -- Integers may as well be considered atoms for this exercise. end if end function function minimum(object a, object b) integer tya, tyb tya = typeof(a) tyb = typeof(b) if tya = tyb then if tya = 0 then if b < a then a = b end if else for i = 1 to tya do a[i] = minimum(a[i], b[i]) end for end if elsif tyb < tya then a = b -- assume atom is less than sequence end if return a end function For such a simple problem, this is quite a monster! :) -- Carl R White E-mail...: cyrek- at -bigfoot.com -- Remove the hyphens before mailing. Ta :) Url......: http://www.bigfoot.com/~cyrek/ "Ykk rnyllaqur rgiokc cea nyemdok ymc giququezka caysgr." - B.Q.Vgesa