Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Types in Mathematica

  • To: mathgroup at
  • Subject: [mg62779] Re: Re: Types in Mathematica
  • From: "Steven T. Hatton" <hattons at>
  • Date: Mon, 5 Dec 2005 03:37:38 -0500 (EST)
  • References: <> <dlp2ci$le$> <> <dls4vp$mmc$> <dm1ak3$i1n$> <dmjrb8$5u6$> <dmm2tp$nmo$> <> <dmtbuu$fo6$>
  • Sender: owner-wri-mathgroup at

Kristen W Carlson wrote:

> 5. If I am extending built-in or user-defined functions (like Plus),
> should I restrict users to using a constructor to do that, or extend
> the existing function in the context of my data type with an upvalue?

I'm not sure what you mean here. Can you provide examples?

> But below is an example that admittedly is borderline; altho I wrote
> it as extending Part with an upvalue to extract the numerator or
> denominator of a rational number (created as a new datatype as an
> exercise from M220 the programming course), I can see the other point
> of view--not allowing Part to do this.
> ....
> The datatype is rational[n_Integer, d_Integer] with n and d
> type-checked as integers ....
> makeRational[n_Integer, 0] :=
>   Print["This rational is undefined."] (* better than just getting
> input back as output *)
> (* vs. this approach which Maeder wrote that does automatic type
> conversion;  for a negative denominator, it calls the function again
> and just reverses the signs of the numerator and denominator; that way
> negative rationals are always in the format rational[-n, d]  *)
> makeRational[n_Integer, d_Integer?Negative] := makeRational[-n, d]
> ...
> denominator::"denominator will extract the denominator of the
> rational. You can also use Part[ rational ] , which has been modified
> to work correctly in the context of this package."
> ...
> rational /: (r_rational)[[1]] := numerator[r];  (* numerator is the
> constructor *)
> rational /: (r_rational)[[2]] := denominator[r]; (* denominator is the
> constructor *)

Do you mean "selector" where you wrote "constructor" in the last two

Thanks for posting that example.  It was instructive.

The Mathematica Wiki:
Math for Comp Sci
Math for the WWW:

  • Prev by Date: Re: Turning on numerical computation
  • Next by Date: Re: How would you evaluate this limit in Mathematica
  • Previous by thread: Re: Types in Mathematica
  • Next by thread: Re: Re: Types in Mathematica