Re: use of reals

This is a discussion on Re: use of reals within the ml forums in Programming Languages category; 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 ...

Go Back   Application Development Forum > Programming Languages > ml

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-04-2003, 12:34 PM
Matthias Blume
Guest
 
Default Re: use of reals

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

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 04:13 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.