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.

2
  • 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. Commented Dec 7, 2018 at 7:00
  • 4
    Possible duplicate of Why can't I compare reals in Standard ML? Commented Dec 7, 2018 at 11:16

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.