PDA

View Full Version : standard ML functional programming, Mixed data type operations


edeita2
04-11-2004, 08:19 PM
What`s wrong with this ML code? My function takes one real and one integer, but type casting the integer, then I force the total result as real type, but it gives me errors, errors, errors...here`s the code:
fun mixedAddition(a : real, (b : int):real):real = a + b;

edeita2
04-12-2004, 10:30 PM
Im` done with this problem, here`s the solution:

fun mixedAddition(a : int, b :real ) = a + floor b;
val mixedAddition = fn : int * real -> int
- mixedAddition(3, 3.75);
val it = 6 : int

GnuVince
04-12-2004, 10:42 PM
You also could've converted b to an int or a to a real:

(Int.toRead a) + b;
a + (Real.toInt b);


Not sure if it's exactly like this, I never learned SML, but you could've used that.

edeita2
04-12-2004, 11:09 PM
Originally posted by GnuVince
You also could've converted b to an int or a to a real:

(Int.toRead a) + b;
a + (Real.toInt b);


Not sure if it's exactly like this, I never learned SML, but you could've used that.

it seems like you`re using Java`s data type parser, but I`m not sure either if that lousy ML has that magic...:P

GnuVince
04-13-2004, 01:26 AM
http://www.standardml.org/Basis/real.html#Real:STR:SPEC

What I was looking for are:

Real.fromInt 10;
Real.toInt 3.1416;


And ML is not lousy, that word is best used to describe Java as a matter of a fact.

Ninja40
06-05-2004, 04:37 AM
A more SML (and more powerful) way to solve this problem would be to build a polymorphic variant like :

let scalar = Int of int | Float of float ;

and then perform pattern matching on them for each operation.

And yes, Java is a lousy language in comparison with SML.

jemfinch
06-06-2004, 03:34 PM
No, Ninja40, that wouldn't be a superior solution in this situation. It would be both slower and hard to implement.

Jeremy