0

I have the following code:

datatype complex = RealImg of real * real | Infinity;

fun divisionComplex(RealImg(a, b), RealImg(0.0, 0.0)) = Infinity
fun divisionComplex(RealImg(a, b), RealImg(c, d)) =
    RealImg ((a * c + b * d) / (c * c + d * d), ((b * c) - (a * d))/ (c* c + d * d))

However it fails with this:

Error: syntax error: inserting  EQUALOP

I am very confused. Why does this happen? I know that I can't compare two reals in SML, but how am I supposed to do pattern matching with 0?

Victor Feitosa
  • 487
  • 4
  • 12
  • 1
    This question is answered in [Why can't I compare reals in Standard ML?](https://stackoverflow.com/questions/41394992/why-cant-i-compare-reals-in-standard-ml) under **Why won't reals in patterns work [...]?** Use an epsilon. – sshine Aug 13 '18 at 04:53
  • Thanks! I've marked it. – Victor Feitosa Aug 13 '18 at 15:24

1 Answers1

2

As you said SML doesn't allow to pattern match real numbers, but recommends to use Real.== instead or compare the difference between these number against some delta.

What about just using a mere if statement for this? (also some Infinity cases added just to make the match against function params exhaustive, but feel free to change it, because it doesn't pretend to be correct)

datatype complex = RealImg of real * real | Infinity;

fun divisionComplex(Infinity, _) = Infinity
  | divisionComplex(_, Infinity) = Infinity
  | divisionComplex(RealImg(a, b), RealImg(c, d)) =
    if Real.== (c, 0.0) andalso Real.== (d, 0.0)
    then Infinity
    else
      RealImg ((a * c + b * d) / (c * c + d * d), ((b * c) - (a * d))/ (c* c + d * d))
Igor Drozdov
  • 14,690
  • 5
  • 37
  • 53