| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| ma_friedrich@gmx.de (Martina) writes: > I am working with a program based on SML'97. As I just learned it is > not possible to use "real" to define a datatype, e.g. > datatype <name> = real; > Maybe anyone can explain to me how I can instruct SML'97 to define a > float number. I think you are seriously confused: 1. A "datatype" declaration creates a brand-new type. But the type "real" already exists, so that's probably not what you want anyway. You could make an alias for "real" by declaring type foo = real Doing so, however, is not necessary for being able to work with real numbers in SML programs. 2. A datatype declaration consists of one or more "cases", each case introducing a value constructor. Your attempt above misses the constructor, so this is not even valid SML (regardless of whether or not you write "real" on the right-hand side). You could have written something like: datatype foo = BAR of real This makes a new type "foo" and gives you a constructor named "BAR" which turns reals into foos: BAR 1.0 : foo Conversely, by pattern-matching against BAR you can extract reals from foos: (fn BAR r => r) : foo -> real 3. I don't know what you mean by "define a float number". Maybe you want to "declare a variable of floating-point type"? If so, you could just add a type constraint of the form ": real" wherever it pleases you. Example: fun float_inc (x : real) = x + 1.0 Notice that in this example the compiler would have figured out all by itself that x must have type real. Because of the way overloading resolution works in SML'97, this is not always so. In some cases you must add a type constraint of some sort to make the compiler realize you meant "real" and not, e.g., "int". Example: fun float_add (x : real, y : real) = x + y (BTW, a constraint on either x or y would have been enough.) Matthias |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.