1

This function is part of a string of functions (for a course). It is supposed to take a list of reals [s,a,w,h], and check it against other lists of reals for equality. Those lists of reals are made from converting type racer objects (in the list R::acer) to real lists using racer_stats().

I then want the function to return the Racer that has stats that equal its racer_stats() stats. Unfortunately no matter what I try I cant figure out how to get SML to pattern match [s,a,w,h] as a real list so it will not compare, even when I made a new base case.

Any advice?

fun RFS([s,a,w,h], []) = None
  | RFS([s,a,w,h], R::acer) =
      if ( [s,a,w,h] = racer_stats(R) )
      then R
      else RFS([s,a,w,h], acer);

I also tried:

fun RFS( [0.1, 0.2, 0.3] , []) = None 
  | RFS([s,a,w,h], []) = None
  | RFS([s,a,w,h], R::acer) =
      if ( [s,a,w,h] = racer_stats(R) )
      then R
      else RFS([s,a,w,h], acer);

and got a syntax error.

sshine
  • 15,635
  • 1
  • 41
  • 66
Connorr.0
  • 97
  • 9
  • 4
    `real` is not an equality type (try entering `1.0 = 1.0;` in your interpreter) so neither is `real list`, and matching on reals is a syntax error. You need to come up with a different means of comparison. – molbdnilo Dec 07 '18 at 07:00
  • 4
    Possible duplicate of [Why can't I compare reals in Standard ML?](https://stackoverflow.com/questions/41394992/why-cant-i-compare-reals-in-standard-ml) – Andreas Rossberg Dec 07 '18 at 11:16

1 Answers1

1

Just in case anyone runs into this later. As molbdnilo pointed out reals are not an equality type. To workaround I built the following comparison operator:

fun compy([], []) = true
    | compy([], x) = false
    | compy(x, []) = false
    | compy(x::xx, y::yy) = ( floor(x*100.0) = floor(y*100.0) ) andalso compy(xx, yy);

The *100.0 was because I wanted to compare to within 2 decimal places. I then swapped compy for =

fun RFS([s,a,w,h], []) = None
    | RFS([s,a,w,h], R::acer) = if (compy([s,a,w,h], racer_stats(R)) ) then R
                else  RFS([s,a,w,h], acer);

Thanks to molbdnilo for pointing out that reals are not an equality type!

Connorr.0
  • 97
  • 9